Bar width doesn't work
显示 更早的评论
hey
I have the following very specific problem. I try to plot 10 pars divided in 4 groups.
The first group is 4 bars de second 3 third 2 fourth 1 bar
say: x=[1:10], y=x.^2
then i want it to plot like this:
figure(1), clf(1), hold on
bar(x([1,5,8]),y([1,5,8]),'r')
bar(x([2,6]),y([2,6]),'b')
bar(x(3),y(3),'g')
bar(x([4,7,9,10]),y([4,7,9,10]),'y')
if i do this de barwidths aren't equal how do i get them to be the same width?
回答(1 个)
Mike Garrity
2014-11-6
编辑:Mike Garrity
2014-11-6
The widths of the bars are calculated from the spacing of the XData. You could calculate backwards from the spacing to come up with a BarWidth scale factor to correct for it, but generally the simplest approach is to use the same XData for all of your bar plots. You can use nans to skip Y values when you do this.
In your case it might look something like this:
y1 = nan(size(y));
y1([1,5,8]) = y([1,5,8]);
y2 = nan(size(y));
y2([2,6]) = y([2,6]);
y3 = nan(size(y));
y3(3) = y(3);
y4 = nan(size(y));
y4([4,7,9,10]) = y([4,7,9,10]);
bar(x,y1,'r')
bar(x,y2,'b')
bar(x,y3,'g')
bar(x,y4,'y')
or this:
y1 = y;
y2 = y;
y3 = y;
y4 = y;
mask1 = [1 0 0 0 1 0 0 1 0 0];
mask2 = [0 1 0 0 0 1 0 0 0 0];
mask3 = [0 0 1 0 0 0 0 0 0 0];
mask4 = [0 0 0 1 0 0 1 0 1 1];
y1(~mask1) = nan;
y2(~mask2) = nan;
y3(~mask3) = nan;
y4(~mask4) = nan;
bar(x,y1,'r')
bar(x,y2,'b')
bar(x,y3,'g')
bar(x,y4,'y')

类别
在 帮助中心 和 File Exchange 中查找有关 2-D and 3-D Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!