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

조회 수: 175 (최근 30일)
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 CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by