How can I change the color of specific boxcharts?

78 ビュー (過去 30 日間)
Rafael Cordero
Rafael Cordero 2021 年 12 月 2 日
コメント済み: Adam Danz 2021 年 12 月 2 日
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!

回答 (1 件)

Adam Danz
Adam Danz 2021 年 12 月 2 日
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 件のコメント
Rafael Cordero
Rafael Cordero 2021 年 12 月 2 日
Woops, I meant to say b = boxchart(...), not subplot(...) = boxchart
Adam Danz
Adam Danz 2021 年 12 月 2 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by