在Matlab中,如何在同一图中绘制多个直方图并使用不同颜色?

6

我有一个600x24的矩阵a,在MATLAB中,我想在同一张图中绘制每一列的直方图,但是用不同颜色,我使用了下面的代码,但是没有得到彩虹色,请求帮助。

```matlab hold on; for i = 1:24 histogram(a(:,i),'FaceColor',hsv2rgb([i/24 1 1])); end hold off; ```
col = hsv(24);

hold on;

for m = 1:24
hist(a(:,m), 50);
h = findobj(gca,'Type','patch');
set(h,'FaceColor', col(m,:),'EdgeColor',col(m,:));
alpha(0.3);
end

hold off;
1个回答

6

MATLAB的hist()函数作用于矩阵,并分别处理矩阵的每一列。而bar()函数可以用于绘制直方图并适当着色。因此,您应该能够通过使用以下方法获得相同的结果:

[h,x] = hist(a,50); % histogram of every column and the bins vector
bar(x,h);           % plot histograms

% create a legend
l = cell(1,24);
for n=1:24, l{n} = num2str(n), end;
legend(l);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接