Plot contours from counts of a scatter plot
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi everyone, I'm fairly new to Matlab and i'm not sure how to obtain what i what i want.
I made this graphics where i'm trying to match some datas with my simulation. The prolem is that i'm simulating thtat same amounts of point as the ones in the data set (about 100) but has some statistic problems. So i wanted to simulate more points (about 1000) to use contour lines to show where i have different density of simulated points. This is where i ran into trubles.
I looked into the contour(Z) function, but i'm not sure how to make the Z matrix. I have two vectors, x and y, with the coordinates of my simulated point (and anoters set of twwo vectors for the datas).
I was thinking about using something lixe this to:
Z = histcounts2(x, y, 'BinWidth', [n, n], 'XBinLimits', [x1 x2], 'YBinLimits', [y1 y2])
But I'm not sure wich BinWidht to use to obtain what I want, or even if ot's the best way to go about it.
Any pointers would be really appreciated,
Thakns to all
0 commentaires
Réponses (1)
Star Strider
le 23 Août 2024
Modifié(e) : Star Strider
le 23 Août 2024
Perhaps something like this —
x = linspace(0.65 , 0.8, 100);
y = 0.25*randn(size(x))+12;
figure
scatter(x, y, 'filled')
Ax = gca;
Ax.YDir = 'reverse';
[z,xe,ye] = histcounts2(x, y);
figure
contourf(xe(1:size(z,1)), ye(1:size(z,2)), z.', 'ShowText',1)
colormap(turbo)
Experiment with your data.
EDIT — (23 Aug 2024 at 13:33)
Minor tweaks to get the corect contourf plot.
.
2 commentaires
Star Strider
le 28 Août 2024
Modifié(e) : Star Strider
le 28 Août 2024
My pleasure!
The contour function automatically chooses several contour levels to draw the contours. To produce only one contour level, set a speciifiic threshold (arbitrarily chosen as ‘[0.01 0.01]’ here) to draw only contours at that level, and I added an argument to outline them with a black solid line. There are only 4 levels iin this example (the result of the unique call), so I chose one that would include all of them greater than 0. Choose the level that gives you the result you want, and the line colour you want. If you want more than one level, for example at 0.5 and 1.5, specify them as [0.5 1.5], and let contour choose the line colours.
xdata = linspace(0.65 , 0.8, 100);
ydata = 0.25*randn(size(xdata))+12;
x = linspace(0.65 , 0.8, 100);
y = 0.25*randn(size(x))+12;
figure
Ax = gca;
Ax.YDir = 'reverse';
scatter (x, y, 'MarkerEdgeColor',"k", 'MarkerFaceColor',"r");
hold on
scatter (xdata, ydata, 'MarkerEdgeColor',"b");
hold on
XEdges = [0.65:0.008:0.8];
YEdges = [11.3:0.04:13.2];
[z,xe,ye] = histcounts2(x, y, XEdges,YEdges);
contour(xe(1:size(z,1)), ye(1:size(z,2)), z.', [1 1]*0.01, '-k') % Plot contour lines based on histogram counts
colormap(turbo) % Apply a colormap
colorbar % Add a color bar to the plot
[ubar] = unique(z(:))
The unique function can help you understand what the histcounts2 function is returning.
EDIT — Corrected typographical errors. (At least the ones I saw.)
.
Voir également
Catégories
En savoir plus sur Data Distribution Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!