Plot contours from counts of a scatter plot (2024)

19 views (last 30 days)

Show older comments

Chiara on 23 Aug 2024 at 12:24

  • Link

    Direct link to this question

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

  • Link

    Direct link to this question

    https://se.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://se.mathworks.com/matlabcentral/answers/2147504-plot-contours-from-counts-of-a-scatter-plot#answer_1504304

  • Link

    Direct link to this answer

    https://se.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://se.mathworks.com/matlabcentral/answers/2147504-plot-contours-from-counts-of-a-scatter-plot#comment_3248414

  • Link

    Direct link to this comment

    https://se.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://se.mathworks.com/matlabcentral/answers/2147504-plot-contours-from-counts-of-a-scatter-plot#comment_3248429

  • Link

    Direct link to this comment

    https://se.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
30 Keto Christmas Recipes! Low Carb Holiday Food So Fabulous You'll Never Miss The Carbs
Christmas pudding | Jamie Oliver Christmas recipes
Genesis Parsippany
How To Do A Springboard Attack In Wwe 2K22
Free Atm For Emerald Card Near Me
Gunshots, panic and then fury - BBC correspondent's account of Trump shooting
Ncaaf Reference
Strange World Showtimes Near Cmx Downtown At The Gardens 16
Danielle Longet
Craigslist Dog Kennels For Sale
Newgate Honda
Vcuapi
Huge Boobs Images
Sivir Urf Runes
50 Shades Darker Movie 123Movies
Metro Pcs.near Me
What Is Vioc On Credit Card Statement
Ein Blutbad wie kein anderes: Evil Dead Rise ist der Horrorfilm des Jahres
Kcwi Tv Schedule
Conan Exiles Sorcery Guide – How To Learn, Cast & Unlock Spells
Clare Briggs Guzman
Sullivan County Image Mate
Used Safari Condo Alto R1723 For Sale
Azur Lane High Efficiency Combat Logistics Plan
Craigslist Illinois Springfield
Walgreens Bunce Rd
Apartments / Housing For Rent near Lake Placid, FL - craigslist
Craigslist Lake Charles
Craig Woolard Net Worth
1145 Barnett Drive
Yayo - RimWorld Wiki
Skepticalpickle Leak
Uno Fall 2023 Calendar
Desales Field Hockey Schedule
Devargasfuneral
O'reilly's Wrens Georgia
EST to IST Converter - Time Zone Tool
All Things Algebra Unit 3 Homework 2 Answer Key
Kelly Ripa Necklace 2022
Boone County Sheriff 700 Report
Cygenoth
Craigslist Com Panama City Fl
Cnp Tx Venmo
Nail Salon Open On Monday Near Me
Frigidaire Fdsh450Laf Installation Manual
Used Auto Parts in Houston 77013 | LKQ Pick Your Part
Tyrone Dave Chappelle Show Gif
Grandma's Portuguese Sweet Bread Recipe Made from Scratch
Tenichtop
Loss Payee And Lienholder Addresses And Contact Information Updated Daily Free List Bank Of America
Elizabethtown Mesothelioma Legal Question
Latest Posts
Article information

Author: Nicola Considine CPA

Last Updated:

Views: 6338

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Nicola Considine CPA

Birthday: 1993-02-26

Address: 3809 Clinton Inlet, East Aleisha, UT 46318-2392

Phone: +2681424145499

Job: Government Technician

Hobby: Calligraphy, Lego building, Worldbuilding, Shooting, Bird watching, Shopping, Cooking

Introduction: My name is Nicola Considine CPA, I am a determined, witty, powerful, brainy, open, smiling, proud person who loves writing and wants to share my knowledge and understanding with you.