我试图在一个图中绘制5个直方图。为了使这个可读,我想到使用'累积'(不确定这是否是正确的术语)条形图,但在MATLAB中找不到简单的实现。有吗?
以下是我所说的一个例子:
a=1:5
b=5:-1:1
当我尝试一个普通的条形图时,它看起来像这样:
bar([a ;b]')
我无法通过'保持'两次使用'bar'来实现这一点,因为其中一个条形图将覆盖另一个。
ps:如果有更好的方法在绘图上绘制多个直方图,除了使用密度曲线,请随时提出建议:)
答案 0 :(得分:4)
我相信你可能在寻找:
bar([a ;b]', 'stacked')
===编辑1 ===
仔细观察您的绘图,您可能也在寻找一种不那么传统的"堆叠"你可以通过叠加这两个条来实现:
a=(1:5)';
b=(5:-1:1)';
c = a;
c(b<=a)=nan;
figure;
subplot(1,2,1);
bar([a,b]);
subplot(1,2,2);
hold on;
bar(a,'b');
bar(b,'r');
bar(c,'b');
hold off
===编辑结束1 ===
===编辑2 ===
这里是我在评论中建议将这个想法扩展到任意数量的系列的实现。基本上,每一层(从最高到最小)都在一个循环中绘制(使用补丁)。虽然这不再是Matlab条形图(据我所知,由于无法通过uistack访问给定系列的独立水平条,因此无法解决您的问题),结果图符合您给出的描述。
function testBarOverlay()
% data initialization
nbSeries = 8;
nbBars = 5;
xt = 1:nbBars;
data = rand( nbBars, nbSeries );
%draw (then erase) figure and get the characteristics of Matlab bars (axis, colors...)
figure;
h = bar( data( :, 1 ) );
width = get( h, 'barWidth' );
delete( h );
% sort in order to start painting the tallest bars
[ sdata, idx ] = sort( data, 2 );
% get the vertices of the different "bars", drawn as polygons
x = [ kron( xt, [1;1] ) - width / 2; kron( xt, [1;1] ) + width / 2 ];
% paint each layer, starting with the 'tallest' ones first
for i = nbSeries : -1 : 1
y = [ zeros( nbBars, 1 ), sdata( :, i ), sdata( :, i ), zeros( nbBars, 1 ) ]';
p = patch( x, y, 'b' );
set( p, 'FaceColor', 'Flat', 'CData', idx( :, i )' );
end
end
===编辑结束2 ===