【发布时间】:2011-03-04 22:05:06
【问题描述】:
我想在一个图中绘制多个直方图,就像 subplot 命令一样,只使用直方图。有没有办法在 MATLAB 中做到这一点?
【问题讨论】:
我想在一个图中绘制多个直方图,就像 subplot 命令一样,只使用直方图。有没有办法在 MATLAB 中做到这一点?
【问题讨论】:
在不同的子情节中?
subplot(2,1,1)
hist(...)
subplot(2,1,2)
hist(...)
【讨论】:
可以使用 hold on 将多个直方图放在一个图中。但是,您需要在绘制下一个直方图之前更改第一个直方图的颜色。
x1 = randn(1000,1);x2 = 1 + randn(1000,1);
hist(x1,100), hold on
h = findobj(gca,'Type','patch');
set(h,'FaceColor','r','Edgecolor','c')
hist(x2,100)
您在比较直方图时应该小心,因为直方图箱是单独生成的。
我使用以下补充来解决这个问题:
x1 = randn(1000,1);x2 = 1 + randn(1000,1);
xrangel = min(min(x1),min(x2));
xrangeh = max(max(x1),max(x2));
x1_tmp = x1(x1>=xrangel & x1<=xrangeh);
x2_tmp = x2(x2>=xrangel & x2<=xrangeh);
xbins = xrangel:(xrangeh - xrangel)/res:xrangeh;
hist(x1_tmp,xbins)
hold on
h = findobj(gca,'Type','patch');
% some additional coloring to help visibility
set(h,'FaceColor','c','EdgeColor',[0 0.99 0],'LineWidth',1.2,'LineStyle','-','EdgeAlpha',0.89);
hist(x2_tmp,xbins)
【讨论】: