Plot contours from counts of a scatter plot (2024)

21 views (last 30 days)

Show older comments

Chiara on 23 Aug 2024 at 12:24

  • Link

    Direct link to this question

    https://connections.mathworks.com/matlabcentral/answers/2147504-plot-contours-from-counts-of-a-scatter-plot

  • Link

    Direct link to this question

    https://connections.mathworks.com/matlabcentral/answers/2147504-plot-contours-from-counts-of-a-scatter-plot

Edited: Star Strider on 28 Aug 2024 at 19:31

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.

Plot contours from counts of a scatter plot (2)

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 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Answers (1)

Star Strider on 23 Aug 2024 at 13:14

  • Link

    Direct link to this answer

    https://connections.mathworks.com/matlabcentral/answers/2147504-plot-contours-from-counts-of-a-scatter-plot#answer_1504304

  • Link

    Direct link to this answer

    https://connections.mathworks.com/matlabcentral/answers/2147504-plot-contours-from-counts-of-a-scatter-plot#answer_1504304

Edited: Star Strider on 23 Aug 2024 at 13:33

Open in MATLAB Online

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';

Plot contours from counts of a scatter plot (4)

[z,xe,ye] = histcounts2(x, y);

figure

contourf(xe(1:size(z,1)), ye(1:size(z,2)), z.', 'ShowText',1)

colormap(turbo)

Plot contours from counts of a scatter plot (5)

Experiment with your data.

EDIT — (23 Aug 2024 at 13:33)

Minor tweaks to get the corect contourf plot.

.

2 Comments

Show NoneHide None

Chiara on 28 Aug 2024 at 18:45

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/2147504-plot-contours-from-counts-of-a-scatter-plot#comment_3248414

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/2147504-plot-contours-from-counts-of-a-scatter-plot#comment_3248414

Moved: Voss on 28 Aug 2024 at 18:47

Open in MATLAB Online

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

Plot contours from counts of a scatter plot (7)

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?

Plot contours from counts of a scatter plot (8)

Plot contours from counts of a scatter plot (9)

Star Strider on 28 Aug 2024 at 19:28

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/2147504-plot-contours-from-counts-of-a-scatter-plot#comment_3248429

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/2147504-plot-contours-from-counts-of-a-scatter-plot#comment_3248429

Edited: Star Strider on 28 Aug 2024 at 19:31

Open in MATLAB Online

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

Plot contours from counts of a scatter plot (11)

[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.)

.

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphics2-D and 3-D PlotsContour Plots

Find more on Contour Plots in Help Center and File Exchange

Tags

  • 'countours' 'contour plot' 'histcounts' 'counts'

Products

  • MATLAB

Release

R2020b

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Plot contours from counts of a scatter plot (12)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Plot contours from counts of a scatter plot (2024)

References

Top Articles
Latest Posts
Article information

Author: Rubie Ullrich

Last Updated:

Views: 6329

Rating: 4.1 / 5 (52 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Rubie Ullrich

Birthday: 1998-02-02

Address: 743 Stoltenberg Center, Genovevaville, NJ 59925-3119

Phone: +2202978377583

Job: Administration Engineer

Hobby: Surfing, Sailing, Listening to music, Web surfing, Kitesurfing, Geocaching, Backpacking

Introduction: My name is Rubie Ullrich, I am a enthusiastic, perfect, tender, vivacious, talented, famous, delightful person who loves writing and wants to share my knowledge and understanding with you.