Borrar filtros
Borrar filtros

Plot contours from counts of a scatter plot

7 visualizaciones (últimos 30 días)
Chiara
Chiara el 23 de Ag. de 2024
Editada: Star Strider el 28 de Ag. de 2024
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

Respuestas (1)

Star Strider
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
Chiara
Chiara el 28 de Ag. de 2024
Movida: Voss el 28 de Ag. de 2024
Thanks for your help!
I tried something similar to your solution and it kind of works.
Probably I wasn't clear in my question: I want isolines that show me where the red dots are. The ending goal is to do a simulation with around 2000/3000 simulated points and to plot only the isolines, insted of the red dots, over the blu dots (datas)
I tried this code:
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.') % Plot contour lines based on histogram counts
colormap(turbo) % Apply a colormap
colorbar % Add a color bar to the plot
i don't understand why i got multiple groups of isolines. Also, a bit of a mistery, if i run the same code on my dekspot Matalb (R2020b) i get a different result. Even more a mystery: the figure is different every time i run it, without changing anything. And I load the same set with the datas and the simulaton points.
Any idea what is happening?
Star Strider
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(:))
ubar = 4x1
0 1 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
The unique function can help you understand what the histcounts2 function is returning.
EDIT — Corrected typographical errors. (At least the ones I saw.)
.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Distribution Plots en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by