如何在 MATLAB 中增加条形图中条形的宽度而不导致条形重叠?下面的脚本增加了条形宽度,但条形重叠:
graph = [ 1 2 ; 3 4 ; 5 6 ; 7 8 ];
bar(graph,'BarWidth',2);
如何在 MATLAB 中增加条形图中条形的宽度而不导致条形重叠?下面的脚本增加了条形宽度,但条形重叠:
graph = [ 1 2 ; 3 4 ; 5 6 ; 7 8 ];
bar(graph,'BarWidth',2);
我知道这样做的唯一方法是通过多次调用 bar。
function h=BarSpecial(data, overallWidth )
colour = {'r','b'};
[r,c] = size(data);
h = zeros(c,1);
width = overallWidth / c;
offset = [-width/2 width/2];
for i=1:c
h(i) = bar(data(:,i),'FaceColor',colour{i},'BarWidth',width);
set(h(i),'XData',get(h(i),'XData')+offset(i));
hold on
end
end
下面将生成一个条形图,其中条形占据总空间的 90%。
BarSpecial(graph,0.9)
编写的函数 BarSpecial 不是通用的,但可以扩展以处理更广泛的输入数据。
默认宽度为 = 0.8。
如果宽度为 1,则组中的条相互接触。
值 > 1 会产生重叠条。
设置宽度小于 1。例如
figure; bar(graph,0.4);