Contour plot of bivariate distributions from XY data (not independent)?

76 ビュー (過去 30 日間)
Chris
Chris 2021 年 3 月 5 日
コメント済み: Cris LaPierre 2021 年 10 月 9 日
I've got two arrays that represent X and Y data (a pair that one could use for a traditional scatter as there is correlation between them) but I'm specifically interested in the distribution/density. I want to plot a smooth contour and I've been able to get the expected plot in Python using Seaborn's kdeplot function (figure A below). However, I do need to do this in MATLAB too and I've not managed to come across a method that yields a similar result yet. Things tried:
  • Using histogram2 (figure B). What it shows is not wrong but I don't want the discreet bins. Figure B obtained using
h = histogram2(x,y,100,'DisplayStyle','tile','ShowEmptyBins','on','EdgeColor','none');
  • I've tried an old answer for plotting a 2D Kernel density from here. Now this was never going to produce the right joint distribution because it does come with the disclaimer that it assumes that the two variables are independent of each other, which isn't the case here. However, it does plot a smooth contour with controurf but after playing around with ksdensity and modifications to the combined pdf, I didn't manage to get it right. Figure C produced via:
% Estimate a continuous pdf from the discrete data
[pdfx xi]= ksdensity(x);
[pdfy yi]= ksdensity(y);
% Create 2-d grid of coordinates and function values, suitable for 3-d plotting
[xxi,yyi] = meshgrid(xi,yi);
[pdfxx,pdfyy] = meshgrid(pdfx,pdfy);
% Calculate combined pdf, under assumption of independence
pdfxy = pdfxx.*pdfyy;
% Plot the results
figure
contourf(xxi,yyi,pdfxy)
[bandwidth,density,X,Y]=kde2d(data,2^6);
%plot the data and the density estimate
contourf(X,Y,density,'EdgeColor','none')
I'm also attaching the set of X Y values used for all of them, if anyone is able/willing to give it a try. Any suggestions on how I could get something similar to the kdeplot output, would be much appreciated.

採用された回答

Cris LaPierre
Cris LaPierre 2021 年 3 月 6 日
編集済み: Cris LaPierre 2021 年 3 月 6 日
Definitely not an expert when it comes to this, but this sounded interesting so I thought I'd take a stab at it. I found the Plot Kernel Density Estimate of Bivariate Data example on the ksdensity documentation page key in coming up with this solution.
First, I created vectors of x and y values based on image A. I used the default number of points for linspace (100), but you can modify this if you want. I then modified the linked example to work with your data.
load xy_contour.mat
% Define grid
xgrid=linspace(0.00225,0.0045);
ygrid=linspace(350,700);
[x1,y1] = meshgrid(xgrid, ygrid);
% Perform kernel density estimate
% [x y] is actual data, xi is the desired grid points to evaluate
% f is an estimate of the density, ep(:,1) is the X location, ep(:,2) is the y location
xi = [x1(:) y1(:)];
[f,ep]=ksdensity([x y],xi); % remove the outputs to see a 3D plot of the distribution
% format data in matrix for contourf and plot
X = reshape(ep(:,1),length(xgrid),length(ygrid));
Y = reshape(ep(:,2),length(xgrid),length(ygrid));
Z = reshape(f,length(xgrid),length(ygrid));
contourf(X,Y,Z,10)
ax=gca;
ax.XAxis.Exponent = 0;
xtickformat('%.4f')
xlabel('col1')
ylabel('col2')
colorbar
See the contourf documentation page for settings on number of levels, etc.
Anything set to nan will not be colored. If you use a vector to set the levels, it is easy to set it to white. Here's an example
figure
% set Z values less than 1.2 to NaN
Z(Z<1.2)=nan;
contourf(X,Y,Z, 1.2:2:12) % vector defines the levels. Modify as desired
ax=gca;
ax.XAxis.Exponent = 0;
xtickformat('%.4f')
xlabel('col1')
ylabel('col2')
The choppiness seen here is a function of the grid resolution. Increase the number of points in xgrid and ygrid to make it smoother.
  5 件のコメント
elahe Noshad
elahe Noshad 2021 年 10 月 9 日
Thanks. Do you mean that I cannot choose the desired color to the contour plots?
Cris LaPierre
Cris LaPierre 2021 年 10 月 9 日

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeContour Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by