Hello Community,
I am trying to plot a bar chart and to colour the bars according to a rule related to the values for the 'Y' axis. So, if the Y value is a minus figure, the bar should be coloured grey, and if positive, should be coloured black. I have tried the following (and several variations):
% Colouring bars
if y <0;
bar(y,'RGB::Grey');
elseif y >= 0;
bar(y,'RGB::Black');
end
but with no luck. Everything else about the plotting that I am doing works fine.
I'm sure this will be simple for the more experienced, so could anyone suggest a fix to help me please?
Thank you,
10B.

 채택된 답변

Mike Garrity
Mike Garrity 2016년 1월 13일

0 개 추천

I would suggest the approach I described in this blog post. In this case, it would look something like this:
x = 1:18;
y = randn(1,18);
mask = y > 0;
y1 = y;
y1(mask) = nan;
bar(x,y1,'FaceColor','red')
hold on
y2 = y;
y2(~mask) = nan;
bar(x,y2,'FaceColor','green')

댓글 수: 3

Image Analyst
Image Analyst 2016년 1월 13일
Mike, I think the best way would be if bar() took an argument that specified the color of each bar. Currently the color you send in applies to all bars. What it should do is to take an array, just like scatter() does, which is an array of colors so that you can specify the color of each, just like scatter() already does. So if you send in only one color, then all bars are that color but if you send in an N-by-3 array, it will give those colors to the corresponding N bars. Do you think they could make that improvement, to bring it into alignment with what scatter already does?
Mike Garrity
Mike Garrity 2016년 1월 13일
Yeah, that's on our list, but I'm afraid it's not very close to the top of the list.
10B
10B 2016년 1월 14일
Hello Mike,
Thanks for your informative answer and link to the blog information (hadn't seen that article).
To be fair, I have accepted your answer, but Star Strider's answer helped as well as this gave me the colouring options that I wanted, so I merged the best parts of the two to get the following:
figure('Color',[1 1 1]);
cm = [0.90 0.90 0.90; 0 0 0];
mask = y > 0;
y1 = y;
y1(mask) = nan;
bar(x,y1,'FaceColor',cm(1,:))
hold on
y2 = y;
y2(~mask) = nan;
bar(x,y2,'FaceColor',cm(2,:))
hold off
With only a couple of tweaks to the colouring values 'cm'.
This now does exactly what I want - Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Star Strider
Star Strider 2016년 1월 13일
편집: Star Strider 2016년 1월 13일

1 개 추천

This seems to work, at least on my test data:

x = 1:10;
y = randi([-9 9], 1, 10);
yp = y>0;                                       % Logial Indices: Positive ‘y’
yn = y<0;                                       % Logial Indices: Negative ‘y’
cm = [0.5 0.5 0.5; 0 0 0];                      % Colour Matrix
figure(1)
bar(x(yn), y(yn), 'FaceColor',cm(1,:))
hold on
bar(x(yp), y(yp), 'FaceColor',cm(2,:))
hold off
set(gca, 'XTick', x)

댓글 수: 2

10B
10B 2016년 1월 14일
편집: 10B 2016년 1월 14일
Excellent information Star Strider - thanks a lot for your help. You will see from the comment above that I merged the two bits of code to get what I wanted, and yours in particular helped with the correct colouring.
Thanks very much!
Star Strider
Star Strider 2016년 1월 14일
My pleasure!
A Vote would be appreciated!

댓글을 달려면 로그인하십시오.

카테고리

제품

질문:

10B
2016년 1월 13일

댓글:

2016년 1월 14일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by