Matlab 2-D密度图

3

我将尝试绘制一张密度图,数据包含两个不同范围的列。RMSD 列的取值范围为 [0-2],Angle 列的取值范围为 [0-200]。

我的文件数据如下:

0.0225370 37.088  
0.1049553 35.309  
0.0710002 33.993  
0.0866880 34.708  
0.0912664 33.011  
0.0932054 33.191  
0.1083590 37.276  
0.1104145 34.882  
0.1027977 34.341  
0.0896688 35.991  
0.1047578 36.457  
0.1215936 38.914  
0.1105484 35.051  
0.0974138 35.533  
0.1390955 33.601  
0.1333878 32.133  
0.0933365 35.714  
0.1200465 33.038  
0.1155794 33.694  
0.1125247 34.522  
0.1181806 37.890  
0.1291700 38.871  
  1. I want both x and y axis to be binned 1/10th of the range
  2. The 0 of both the axis to be starting in the same
  3. Print the number of elements in each grid of the matrix like this and make a density plot based on these number of elements

       0 0.1 0.2 (RMSD)   
    0  0 1 3
    20 2 0 4
    40 1 0 5
    60 0 0 2
    (Angle)
    
我可以找到一些方法来进行1D分箱,但是我却不知道如何从这些值中制作密度图,更没有尝试过2D分箱和绘图。
感谢您的帮助。
2个回答

1

我认为你想要hist3。假设你想要指定边缘(而不是箱子中心)的箱子,使用

result = hist3(data, 'Edges', {[0 .1 .2], [0 20 40 60]}).';

其中data表示您的数据。

根据链接的文档:

hist3(X,'Edges',edges),其中edges是一个包含两个数值向量的单元格数组,其值单调不降,使用一个二维网格的箱子,在第一维上的边缘为edges{1},在第二维上的边缘为edges{2}。如果(ij)箱中包含值X(k,:),则第(ij)箱如下:

edges{1}(i) <= X(k,1) < edges{1}(i+1)
edges{2}(j) <= X(k,2) < edges{2}(j+1)

使用您的示例数据,将会得到以下结果:

result =
     0     0     0
     8    14     0
     0     0     0
     0     0     0

我正在进行以下操作:加载rmsd_angle_all_combined.list;
result = hist3(rmsd_angle_all_combined, 'Edges',{[0 .1 .2 .4 .6 .8 1 1.2 1.4 1.6 1.8 2], [0 20 40 60 80 100 120 140 160 180 200]}).'; imagesc
这会产生一张两个轴上都有0-70的图片。这里出了什么问题?
- user1504209
@user1504209 我不知道... hist3 的输出大小是由边的数量确定的。 - Luis Mendo

0

对于那些没有 统计和机器学习工具箱 来运行双变量直方图 (hist3) 的人,使用另一种方法来解决2-D hist问题可能更实际。以下函数将生成相同的输出

function N = hist3_alt(x,y,edgesX,edgesY)
N = zeros(length(edgesY)-1,length(edgesX)-1);
[~,~,binX] = histcounts(x,edgesX);
for ii=1:numel(edgesX)-1
    N(:,ii) = (histcounts(y(binX==ii),edgesY))';
end

这很简单高效。然后你可以像这样运行函数:

N = hist3_alt(x,y,[0:0.1:2],[0:20:200])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接