在matlab中并排乘以直方图

时间:2011-06-24 18:02:27

标签: matlab histogram

我想在matlab中生成如下的情节。

enter image description here

或者可能是这样的 enter image description here

2 个答案:

答案 0 :(得分:15)

您可以使用bar(...)或hist(...)来获得所需的结果。请考虑以下代码,结果如下所示:

% Make some play data:
x = randn(100,3);
[y, b] = hist(x);

% You can plot on your own bar chart:
figure(82);
bar(b,y, 'grouped');
title('Grouped bar chart');

% Bust histogram will work here:
figure(44);
hist(x);
title('Histogram Automatically Grouping');

% Consider stack for the other type:
figure(83);
bar(b,y,'stacked');
title('Stacked bar chart');

Group Bar Result Histogram Results Stacked Bar Result

如果您的数据大小不同并且您想要做直方图,您可以自己选择容器以强制将hist(...)结果设置为相同大小,然后将结果绘制在矩阵中,如:

data1 = randn(100,1);       % data of one size
data2 = randn(25, 1);       % data of another size!

myBins = linspace(-3,3,10); % pick my own bin locations

% Hists will be the same size because we set the bin locations:
y1 = hist(data1, myBins);   
y2 = hist(data2, myBins);

% plot the results:
figure(3);
bar(myBins, [y1;y2]');
title('Mixed size result');

得到以下结果:

enter image description here

答案 1 :(得分:1)

hist是否已经完成了第一个?

来自help hist

N = HIST(Y) bins the elements of Y into 10 equally spaced containers
and returns the number of elements in each container.  If Y is a
matrix, HIST works down the columns.

第二次看help bar