MATLAB Answers

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

807 views (last 30 days)
the cyclist
the cyclist on 13 Nov 2014
Commented: 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.

Accepted Answer

Star Strider
Star Strider on 13 Nov 2014
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

the cyclist
the cyclist on 14 Nov 2014
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!
Star Strider
Star Strider on 14 Nov 2014
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!