How can I specifiy the colors when using the group by color option in boxchart?

157 ビュー (過去 30 日間)
Rouven Kenville
Rouven Kenville 2020 年 12 月 18 日
コメント済み: Dave B 2021 年 9 月 9 日
Hi everyone,
I am using the boxchart function to plot some grouped data. It works fine, but I cannot find a way to specify the colors of the boxes? I tried it like this:
boxchart(trialtable_NOV.Name,trialtable_NOV.HB,'BoxFaceColor',{[0 0 1],[1 0 0]},'GroupByColor',trialtable_NOV.COND);
This does not work. I also tried to access the axes.children, but it only lets me set one color and if I change it, it changes the color of all boxes which loses the color distinction between groups. I did a quick search online and could not find any solutions, so I hope you can help me out.
Cheers
  2 件のコメント
Rouven Kenville
Rouven Kenville 2020 年 12 月 18 日
Hi I just figured it out. You have to use the seriesindex property and set it to a colour of your choosing.
Jiafeng Ye
Jiafeng Ye 2021 年 9 月 9 日
Hi Rouven, I have the similar problem you used to had. I want to assign the box color mannully for two groups with non-default colors. Would you mind being a bit more specific about how to use the seriousindex to achieve that? Thanks

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

採用された回答

Dave B
Dave B 2021 年 9 月 9 日
BoxChart will use the colors in the active 'colororder', you can change which index they use with SeriesIndex but I'd recommend using the colororder command to change the colors. The bonus of using colororder is that you can have multiple charts (or objects in an axes) that share the same color scheme.
load patients
boxchart(double(Age<30), Systolic, 'GroupByColor', Smoker)
colororder([1 1 0; 0 1 1]);
Alternatively you can set the colors manually:
h=boxchart(double(Age<30), Systolic,'GroupByColor',Smoker);
h(1).BoxFaceColor='r';
h(2).BoxFaceColor='m';
  2 件のコメント
Jiafeng Ye
Jiafeng Ye 2021 年 9 月 9 日
Hi Dave, your answer is very helpful. The colorcorder function completely solved my problem. Thank you very much !
Dave B
Dave B 2021 年 9 月 9 日
Happy to help, colororder is awesome I wish it was easier to find. It's especially great when you have a bunch of plots:
metroclr = validatecolor(["#FFCE00" "#0064B0" "#C04191"], 'multiple');
x=repmat(1:3,100,1);
y=randn(100,3)+x;
nexttile;swarmchart(x, y, 'filled', 'MarkerFaceAlpha', .5)
nexttile;boxchart(y(:), 'GroupByColor', x(:))
nexttile;plot(smoothdata(y), 'LineWidth', 2)
h=legend;
h.Layout.Tile=4;
colororder(metroclr)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by