2013-05-13 149 views
2

我有多个直方图,我想叠加在彼此之上,但我不知道该怎么做。我发现了下面的代码,但我不知道如何修改它以在循环上运行,而不是仅仅运行两个直方图。如何覆盖matlab中的直方图

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

或什么是比较直方图的一种更好的方式,如果他们超过10或20

+2

持有,持有了吗? – 2013-05-13 10:18:02

+1

在您的代码中,yBar总是有相同的长度(10),但我应该怎么做(可能是matlab中的单独条)(http://stackoverflow.com/questions/16313392/separate-bars-in-matlab) – Shai 2013-05-13 11:25:17

回答

0

你可以做到以下几点,虽然它不是唯一的方法:

data = cell(1, N); 
y = cell(1, N); 
yBar = zeros(N, 10); 
for i=1:N 
    data{1, i} = randn(10*round(rand(1,1)), 1); 
    y{1, i} = hist(data{1, i}, myBins); 
    yBar(i, :) = y{1, i}; 
end 
yBar = yBar'; 
figure(3); 
bar(myBins, yBar); 
title('Mixed size result'); 

当然,使用y单元格并不是强制性的,我把它留在那里来实际展示发生了什么。

+0

如果情况并非如此。我每次都会改变...... – Kiarash 2013-05-13 10:50:38

2

你的问题很一般。首先,我不明白你为什么坚持一个for循环。

我个人不喜欢包含的条形图。它很快变得混乱(特别是因为酒吧不在“原始”的位置)

如果你有很多的直方图,我会考虑一个stairstep情节,因为它不会填满剧情区太多。或者你可以拿出你自己的 - 例如使用透明的补丁。

如果它获得了很多曲线,有很多方法可以将它们可视化为Google的“多变量可视化”并且很惊讶。其中最有趣的方式是Chernoff faces

+0

我只是有一堆细菌随时间流逝的速度。并且每次都有速度的直方图。我们观看约40次。所以有40个速度变化的直方图。直方图几乎是钟形曲线,随着时间推移向一侧移动。你认为展示这个的正确方法是什么? – Kiarash 2013-05-13 11:10:01

+0

如果它大致是钟形曲线,我会先画出平均值和标准偏差。 Mabe沿着这条线:http://www.mathworks。com/matlabcentral/fileexchange/27485 – bdecaf 2013-05-13 12:39:18

0

我会建议这个。这很简单,并不需要for循环:

bar([y1.' y2.'],'stacked') 
0

这里有一个方法,这是对我很有用:

enter image description here

我绘制了矩阵ao的每一列的直方图。

的代码是:

for i = 1:size(ao,2) 
     [h, y] = hist(ao(:,i), linspace(-5,10,100)); 
     h = i + (0.95./max(h(:))) .* h; 
     barh(y, h, 'BarWidth', 1, 'BaseValue', i, 'LineStyle', 'none'); 
     hold on; 
    end 
    grid; 

注意,只是改变barhbar将给予同样的事情,但要上下而非左右(即图中的反时针方向旋转90°)。

+0

我从来没有见过这样的人,并觉得很难理解。 – neuronet 2015-04-06 17:33:52

1

现在要容易得多:在

histogram(data1, myBins); 
hold on; 
histogram(data2, myBins);