2013-11-26 61 views
2

我想创建一个条形图,我改变一些酒吧的颜色。 我条形图的代码如下:我怎样才能改变的条形图栏颜色?

y = [0.04552309, -0.001730885, 0.023943445, 0.065564478, 0.032253892, 0.013442562, ... 
     -0.011172323, 0.024595622, -0.100614203, -0.001444697, 0.019383706, 0.890249809]; 
bar(y) 

我希望第一个六小节是黑,最后6条是蓝色的,但我不知道该怎么做。

回答

6

您需要单独绘制出来(但在同一坐标):

bar(1:6,y(1:6),'k') 
hold on 
bar(7:numel(y),y(7:end),'b') 
set(gca,'xtick',1:numel(y)) 

enter image description here

+0

非常感谢! –

+0

@ user2971574欢迎!这很容易:-) –

+0

也许你能回答关于该柱状图:)另外一个问题。我能做些什么来获得y上的不同值?我想,我需要这样的:为I1 = 1 –

0

Bar plot with bars in different colors适应MATLAB的中央执行以下操作:

y= [0.04552309, -0.001730885, 0.023943445, 0.065564478, 0.032253892, 0.013442562, -0.011172323, 0.024595622, -0.100614203, -0.001444697, 0.019383706, 0.890249809]; 
for ii=1:12 
    h = bar(ii-0.5, y(ii)); 
    if ii == 1 
    hold on 
    end 
    if ii<=6 
    col = 'k'; 
    else 
    col = 'b'; 
    end  
    set(h, 'FaceColor', col,'BarWidth',0.4) 
end 
axis([0 12 -0.2 1]) 
hold off 
+0

实际上,Luis的回答非常简单,更有效 – am304

+0

谢谢你的回答! –

0

无需情节分开,这里是简单的解决办法:

y= [0.04552309, -0.001730885, 0.023943445, 0.065564478, 0.032253892, 0.013442562, -0.011172323, 0.024595622, -0.100614203, -0.001444697, 0.019383706, 0.890249809]; 
figure, 
bar(y) 

y1 = zeros(1,length(y)); 
y1(3:5) = y(3:5); 
hold on 
h = bar(y1) 
set(h, 'FaceColor', 'k') 

See output

相关问题