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

5 vues (au cours des 30 derniers jours)
the cyclist
the cyclist le 13 Nov 2014
Commenté : Star Strider le 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.

Réponse acceptée

Star Strider
Star Strider le 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 commentaires
the cyclist
the cyclist le 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 le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by