# Colouring Bars by 'Y' value

1 visualización (últimos 30 días)
10B el 13 de En. de 2016
Comentada: Star Strider el 14 de En. de 2016
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.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Mike Garrity el 13 de En. de 2016
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);
y1 = y;
bar(x,y1,'FaceColor','red')
hold on
y2 = y;
bar(x,y2,'FaceColor','green')
##### 3 comentariosMostrar 1 comentario más antiguoOcultar 1 comentario más antiguo
Mike Garrity el 13 de En. de 2016
Yeah, that's on our list, but I'm afraid it's not very close to the top of the list.
10B el 14 de En. de 2016
Hello Mike,
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];
y1 = y;
bar(x,y1,'FaceColor',cm(1,:))
hold on
y2 = y;
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!

Iniciar sesión para comentar.

### Más respuestas (1)

Star Strider el 13 de En. de 2016
Editada: Star Strider el 13 de En. de 2016

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 comentariosMostrar NingunoOcultar Ninguno
10B el 14 de En. de 2016
Editada: 10B el 14 de En. de 2016
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 el 14 de En. de 2016
My pleasure!
A Vote would be appreciated!

Iniciar sesión para comentar.

### Categorías

Más información sobre Line Plots en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by