Plot contours from counts of a scatter plot
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Star Strider
el 23 de Ag. de 2024
Editada: Star Strider
el 23 de Ag. de 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 comentarios
Star Strider
el 28 de Ag. de 2024
Editada: Star Strider
el 28 de Ag. de 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.)
.
Ver también
Categorías
Más información sobre Data Distribution Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!