Colouring Bars by 'Y' value
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (1)
Star Strider
el 13 de En. de 2016
Editada: Star Strider
el 13 de En. de 2016
1 voto
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 comentarios
Star Strider
el 14 de En. de 2016
My pleasure!
A Vote would be appreciated!
Categorías
Más información sobre Annotations en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
