How can I change the color of specific boxcharts?

69 views (last 30 days)
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!

Answers (1)

Adam Danz
Adam Danz on 2 Dec 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 Comments
Rafael Cordero
Rafael Cordero on 2 Dec 2021
Woops, I meant to say b = boxchart(...), not subplot(...) = boxchart
Adam Danz
Adam Danz on 2 Dec 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.

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!