MATLAB Answers

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

32 views (last 30 days)
Rouven Kenville
Rouven Kenville on 18 Dec 2020
Commented: Dave B on 9 Sep 2021
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.
Jiafeng Ye
Jiafeng Ye on 9 Sep 2021
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

Sign in to comment.

Answers (1)

Dave B
Dave B on 9 Sep 2021
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);
Dave B
Dave B on 9 Sep 2021
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');
nexttile;swarmchart(x, y, 'filled', 'MarkerFaceAlpha', .5)
nexttile;boxchart(y(:), 'GroupByColor', x(:))
nexttile;plot(smoothdata(y), 'LineWidth', 2)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!