[英]How to plot a Diagonal Histogram in Matlab
Given scatter data, or a matrix, I would like to generate a nice plot such as the one shown below, with all 3 histograms and a colored matrix. 给定散射数据或矩阵,我想生成一个很好的图,如下图所示,包含所有3个直方图和一个彩色矩阵。 I'm specifically interested in the diagonal histogram, which ideally, would correspond to the diagonals of a matrix:
我对对角线直方图特别感兴趣,理想情况下,它对应于矩阵的对角线:
Source figure : www.med.upenn.edu/mulab/jpst.html 来源图 : www.med.upenn.edu/mulab/jpst.html
The existing command scatterhist is not that powerful to generate this type of graph. 现有命令scatterhist对于生成此类图形并不是那么强大。 Any ideas?
有任何想法吗?
Thanks! 谢谢!
EDIT : 编辑 :
Following @Cris Luengo's hints, I came up with the following code which does some first work at the inclined histogram: WORK IN PROGRESS (HELP WELCOME)!! 按照@Cris Luengo的提示,我想出了以下代码,它在倾斜的直方图中做了一些第一次工作:正在进行中工作(帮助欢迎)!!
b = [0 1 2 3 4 5 6 7 8 9 10];
h = [0.33477 0.40166 0.20134 0.053451 0.008112 0.000643 2.7e-05 0 0 0 0];
wid = 0.25; bb = sort([b-wid b-wid b+wid b+wid]);
kk = [zeros(numel(h),1) h(:) h(:) zeros(numel(h),1)];
kk = reshape(kk',[1,numel(kk)]);
pp=patch(bb,kk,'b');axis([-.5 5 0 .5])
set(gca,'CameraUpVector',[-1,.08,0]);axis square
EDIT 2: Using rotation 编辑2:使用旋转
phi = pi/4;
R = [cos(phi),-sin(phi);sin(phi),cos(phi)];
rr = [bb' kk'] * R;
bb = rr(:,1); kk = rr(:,2);
patch(bb,kk,'b'); axis([-.5 3 -4 .5])
Here is a recipe to plot the diagonal histogram, if you can do that I'm sure you can figure out the rest too. 这是绘制对角线直方图的一个方法,如果你能做到这一点,我相信你也可以弄清楚其余部分。
Compute the histogram, the bin counts are h
, the bin centers are b
. 计算直方图,箱数是
h
,箱中心是b
。
Build a coordinate matrix, attaching the coordinates of a point on the x-axis at the left and right ends of the histogram: 构建坐标矩阵,在直方图的左端和右端附加x轴上的点的坐标:
coords = [b(:),h(:)]; coords = [coord;b(end),0;b(1),0];
Using patch
you can now plot the histogram as follows: 使用
patch
您现在可以绘制直方图,如下所示:
patch(coords(1,:),coords(2,:));
To plot a rotated histogram you can simply multiply the coords
matrix with a rotation matrix, before using patch
: 要绘制旋转直方图,您可以在使用
patch
之前简单地将coords
矩阵与旋转矩阵相乘:
phi = pi/4; R = [cos(phi),-sin(phi);sin(phi),cos(phi)]; coords = R * coords;
You might need to shift the plot to place it at the right location wrt the other elements. 您可能需要移动绘图以将其放置在其他元素的正确位置。
I recommend that you place all these graphic elements in the same axes object; 我建议您将所有这些图形元素放在同一个轴对象中; you can set the axes' visibility to 'off' so that it works only as a canvas for the other elements.
您可以将轴的可见性设置为“关闭”,以便它仅作为其他元素的画布。
It will be a bit of work to get everything placed as in the plot you show, but none of it is difficult. 将所有内容放置在您显示的图中将会有一些工作,但这些都不困难。 Use the low-level
image
, line
, patch
and text
to place those types of elements, don't try to use the higher-level plotting functions such as plot
, they don't provide any benefits over the low-level ones in this case. 使用低级
image
, line
, patch
text
本来放置这些类型的元素,不要尝试使用更高级别的绘图功能,例如plot
,它们不会提供任何优于其中的低级绘图功能案件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.