how to plot contour lines on Joint Histogram plot

Hi all,
I am plotting joint histogram by using histogram2 as shown in figure.
I need contour lines to differentiate each color which gives clear picture of data distribution means lines to separate each color .
Can anyone help me with this?
my code for this plot as follows:
clf
figure(1)
Yedges = [0.25:.25:100];
Xedges = [50:2.5:350];
h= histogram2(olr,rf,Xedges,Yedges,'DisplayStyle','tile','FaceColor','flat')%,'Normalization','pdf')
colorbar;
colormap(hsv(15))
ylim([0 100])
ax=gca;
ax.CLim=[0 100]
And, The kind of figure I want should look like(plotted in Rstudio):
Thanks,
Utkarsh

2 Kommentare

If you share your data, we can probably be more helpful.
Hi Cris,
Here is the data which I used to plot above histogram plot.
Note:- On the x axis "noaa_olr_dly_1985_2014_AI_grid_masked_JJAS.nc" data is plotted and on the y axis "rf_daily_1985_2014_grid_JJAS.nc" is plotted.
https://drive.google.com/drive/folders/1VJSlRG8ldh_Nh7Nx0hSDs1wVl7wGGghn?usp=sharing

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Cris LaPierre
Cris LaPierre am 6 Mär. 2021
Bearbeitet: Cris LaPierre am 6 Mär. 2021
I'll share a couple thoughts about how I would try to approach this.
In order to create a contour plot, MATLAB requires the inputs to be matrices. Each row corresponds to a value on the Y axis, and each column corresponds to a value on the X axis. This means the data needs to be arranged in an evenly spaced grid.
For your data, that means dividing the XY plane into bins, and counting the number of points in each bin. You can do this using histcounts2. This returns a mxn matrix where the rows corespond to your X bins, and the columns correspond to your Y bins.
Next, create X and Y matrices, also mxn, using meshgrid.
Finally, create your contour plot.
Here's a rough example using random numbers.
% Create random data
x = (randn(20000,1)*40)+200;
y = abs(randn(20000,1))*20;
plot(x,y,'.')
% Create buns and obtain counts
Xedges=linspace(0,400,51);
Yedges=linspace(0,80,21);
N = histcounts2(x,y,Xedges,Yedges);
% use bin edges to find the center of each bin in X and Y
[X,Y] = meshgrid(mean([Xedges(1:end-1);Xedges(2:end)],1),...
mean([Yedges(1:end-1);Yedges(2:end,1)],1));
% Create countor, transponsing N so rows correspond to Y and columns correspond to X
figure
contourf(X,Y,N',5,"ShowText","on")

8 Kommentare

Thanks for sharing your data. By modifying the code I shared above, you can create a contour plot with your data. Just be aware that the final result will depend on how you bin your data.
Here's how I did it.
% bin data
Xedges=linspace(100,350,51);
Yedges=linspace(0,80,21);
N = histcounts2(x,y,Xedges,Yedges);
% Plot contour plot
[X,Y] = meshgrid(mean([Xedges(1:end-1);Xedges(2:end)],1),...
mean([Yedges(1:end-1);Yedges(2:end,1)],1));
levels = 0:2000:26000;
[C,h] = contourf(X,Y,N',levels(1:end-1),...
"ShowText","on","TextList",[levels(2:end-3)]);
% format figure
colormap(parula(length(levels)-1))
ylim([0 15])
l=colorbar
caxis(levels([1 end]))
l.Ticks=[10000 20000];
l.TickLabels = ["10k","20k"];
Hi Cris,
Thanks for helping!
I tried your sample code and keeping your suggestion of binning the data in my mind and I tried multiple set of xedges and yedges but the last contour couldn't go beyong ~15 on the y-axis but if you see my original plot with using histogram2 function it goes beyond 100. So, Y-axis doesn't give sense to my plot.
Here, Y data has max value around 550 but concentration of y data values decreases drastically beyond 100, for which I need contour graph atleast till 100 on the y axis or maybe beyong is better.
Can you add some more suggestions so that I could get the desired plot?
The original plot you showed only went to 5, so I limited the yaxis. Remove this line to have it autoset the limits.
ylim([0 15])
I aleady commented and tried but Y-axis data value doesn't go beyond 15
Looks like it went to 75 to me.
Oh, you're probabaly referring to the contour lines. That's because I hard-coded the levels. Take a look at the corresponding section in the documentation.
Yes y-axis shows 75 max value but according to contour graph it appears no data or constant beyond 15 whatever way it doesn't make sense according to data. If you look at the Matlab plot through histogram2, I need more contour to separate out density to higher y axis values
See my last reply

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by