MATLAB Answers

the cyclist
1

How to change the color of the bars in a bar chart?

Asked by the cyclist
on 13 Nov 2014
Latest activity Commented on by Star Strider
on 14 Nov 2014

Make a simple bar chart (and get its handle):

figure
hb = bar(magic(3));

How can I set the colors of those bars now (after I have made the figure)? I know that I could have inserted another argument into the bar function, but suppose I didn't.

I am specifically asking about MATLAB 2014b.

  0 Comments

Sign in to comment.

1 Answer

Answer by Star Strider
on 13 Nov 2014
 Accepted Answer

Here’s one way:
d = magic(3);
figure(1)
hb = bar(d)
hb(1).FaceColor = 'r';
hb(2).FaceColor = 'm';
hb(3).FaceColor = 'c';

  2 Comments

So simple. I was fooled by the fact that the value of FaceColor is 'flat' by default, so I didn't even bother trying a color there.
Thanks!
My pleasure!
HG2 is proving to be anything but obvious. I just learned a few days ago that it’s possible to get the actual bar position information from 'XData', and to change the tick values on what was previously the x-axis, it’s necessary to set 'YTickLabel' values.
I’m sure other mysteries are yet to be discovered.

Sign in to comment.