Effacer les filtres
Effacer les filtres

How can I change the color of specific boxcharts?

67 vues (au cours des 30 derniers jours)
Rafael Cordero
Rafael Cordero le 2 Déc 2021
Commenté : Adam Danz le 2 Déc 2021
Hello all,
I'm having some problems coloring bar charts.
If I have a subplot which is a boxchart( ) with several little boxcharts that I want to be colored according to varialbe 'col' representing and RGB triplet:
subplot(3, 1, 2); b = boxchart(rand(100, 10), 'BoxFaceColor', col)
Let's say I want to change the color of SOME of these 10 boxcharts to black. The specific boxcharts to recolor could be given by a logical vector 'recolor'
recolor = logical([0 0 1 1 0 1 0 0 1 0]);
Where logical 1 would indicate that that particular subplot should be recolored to black.
How exactly can I perform this recoloring of specific boxcharts?
Thank you!

Réponses (1)

Adam Danz
Adam Danz le 2 Déc 2021
It looks like you're either using "subplot" as a variable name of erroneously assigning boxchart handles to the subplot function.
subplot(3, 1, 2) = boxchart(rand(100, 10), 'BoxFaceColor', col)
One way to control boxchart color is by using groupByColor which is demonstrated in the documentation. That outputs a vector of handles, one for each group. You can change the color using the BoxFaceColor property of each handle but then entire subgroup will always have the same color. One way around that is to make each boxplot its own group.
Alternatively, you could plot each box individually in a loop. That is demonstrated in this answer.
  2 commentaires
Rafael Cordero
Rafael Cordero le 2 Déc 2021
Woops, I meant to say b = boxchart(...), not subplot(...) = boxchart
Adam Danz
Adam Danz le 2 Déc 2021
Another alternative is to use boxplot instead of boxchart. You can edit the edge colors but boxplot does not have an option to fill the boxes.

Connectez-vous pour commenter.

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by