沿第三轴绘制多个标准化直方图

时间:2017-01-01 23:39:59

标签: matlab histogram matlab-figure

我想在单个图中绘制多个直方图,其中直方图沿第三个轴分开。

例如,让我们说我想绘制以下两个直方图(实际上,我想同时绘制10个直方图):

enter image description here

enter image description here

它们由以下代码生成:

histogram(points_inside1,100,'Normalization','pdf')

histogram(points_inside2,100,'Normalization','pdf')

其中' points_inside1'和' points_inside2'是具有10 ^ 5个条目的数组。

我无法bar3工作(我不认为它可以将标准化的直方图作为输入)。

谢谢。

1 个答案:

答案 0 :(得分:1)

要做到这一点,您需要先按histcounts获取直方图数据,然后用bar3绘制:

% some data:
p1 = randn(1000,1)+2;
p2 = randn(1000,1);
% don't use too many bins:
bins = 10;
% get the histogram data:
[N1] = histcounts(p1,bins,'Normalization','pdf');
[N2] = histcounts(p2,bins,'Normalization','pdf');
% plot it:
bar3([N1.' N2.']);

但是,由于X轴未在所有直方图之间对齐,您可能仍会遇到问题,因此您可以将组合直方图的edges用于所有其他直方图:

% get the histogram data:
[~,edges] = histcounts([p1;p2],bins,'Normalization','pdf');
[N1] = histcounts(p1,edges,'Normalization','pdf');
[N2] = histcounts(p2,edges,'Normalization','pdf');
% compute the correct X-tick values:
X = movmean(edges.',2);
% plot it:
b = bar3(X(2:end),[N1.' N2.']);

3D_hist