简体   繁体   English

2D散射的MATLAB等高线图

[英]MATLAB contour plot of 2D scatter

What I want to do is very simple, I just cannot seem to get MATLAB to do it. 我想做的很简单,我似乎无法让MATLAB来做。 I would like to plot contours using my 2D data set. 我想使用2D数据集绘制轮廓。

My data set is large; 我的数据集很大; 2 x 844240. I can do a scatter plot just fine, 2 x844240。我可以做一个散点图,

scatter(Data(1,:), Data(2,:));

Reading through the forums I found Scatter plot with density in Matlab , where a hisogram was plotted. 在论坛上仔细阅读后,我在Matlab中发现了密度分布的散点图 ,其中绘制了直方图。 This would suffice, however, I would like to overlay the plots. 这就足够了,但是,我想覆盖这些情节。

在此处输入图片说明

The issue is that they have different axis, my scatter data has an axis of [0 0.01 0 2500]; 问题是它们的轴不同,我的散点数据的轴为[0 0.01 0 2500]; whereas the histogram is [0 100 0 100]. 而直方图为[0 100 0 100]。

Is there a way to change the axis values of the histogram without modifying the image? 有没有一种方法可以在不修改图像的情况下更改直方图的轴值?

Thanks! 谢谢!

If I understand correctly, you are using hist3 to construct a histogram and then using imagesc to plot it. 如果我理解正确,那么您正在使用hist3来构建直方图,然后使用imagesc进行绘制。 You can use the second output argument of hist3 to get the histogram bin centers, and then pass those on to imagesc , eg 您可以使用hist3的第二个输出参数来获取直方图bin中心,然后将其传递给imagesc ,例如

nBins_x = 100;
nBins_y = 100;
[counts, bin_centers] = hist3(Data, [nBins_x nBins_y]);
x_bin_centers = bin_centers{1};
y_bin_centers = bin_centers{2};
imagesc(x_bin_centers, y_bin_centers, counts)

A couple other notes: 其他一些注意事项:

  • In your case, you will need to transpose your [2 x N] matrix when passing it to hist3 , which expects an [N x 2] matrix. 在您的情况下,将[2 x N]矩阵传递给hist3时需要转置该矩阵,而hist3需要[N x 2]矩阵。

  • imagesc puts the first axis (which I've been calling the "x" axis) on the vertical axis and the second on the horizontal axis. imagesc将第一个轴(我一直称之为“ x”轴)放在垂直轴上,将第二个轴放在水平轴上。 If you want to flip it, you can use: 如果要翻转它,可以使用:

     imagesc(y_bin_centers, x_bin_centers, counts') 
  • If you want to specify the histogram bins explicitly (eg to match your scatterplot) you can specify that in the arguments to hist3: 如果要显式指定直方图箱(例如,与散点图匹配),则可以在hist3的参数中指定:

     x_bin_centers = linspace(0, .01, 100); y_bin_centers = linspace(0, 2500, 100); counts = hist3(Data, {x_bin_centers, y_bin_centers}; 

And if you want a contour plot, you can use (note that contour takes the axes arguments in a different order than imagesc ): 而且,如果要绘制轮廓图,可以使用(请注意, contour以与imagesc不同的顺序接受轴参数):

contour(x_bin_centers, y_bin_centers, counts');

If you are unhappy with the jaggedness of the contours, you may consider using a kernel density estimate instead of a histogram (check out ksdensity) (oops, looks like ksdensity is 1-D only. But there are File Exchange submissions for bivariate kernel density estimation). 如果您对轮廓的锯齿状不满意,则可以考虑使用内核密度估计值而不是直方图(检查ksdensity) (糟糕,看起来ksdensity仅是一维的。但是对于双变量内核密度,存在文件交换提交估计)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM