How to avoid horizontal shifts after applying GroupByColor on boxcharts?
7 ビュー (過去 30 日間)
古いコメントを表示
I'm trying to plot 5 boxcharts with 5 different boxcolors in one figure.
To get different colors im using the GroupByColor function as stated in the documentation.
The application of GroupByColor however leads to a horizontal shift of the boxplots.
How do I have to change my code to avoid the shift of my boxplots?
figure(1)
boxchart(xData,yData,'GroupByColor',xData,'BoxWidth',2)
grid on
'GroupByColor' not applied: correct position, wrong colors
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1361098/image.png)
'GroupByColor' applied: wrong position, correct colors
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1361103/image.png)
0 件のコメント
回答 (2 件)
Adam Danz
2023 年 4 月 19 日
You can create the boxes in a loop,
y = magic(5);
[nrows, ncols] = size(y);
hold on
for i = 1:ncols
boxchart(repelem(i,nrows,1),y(:,1))
end
0 件のコメント
Cris LaPierre
2023 年 4 月 19 日
編集済み: Cris LaPierre
2023 年 4 月 19 日
Typically, groups of boxplots share the same x value (see here and here). So what is happening is MATLAB is making room for one boxplot for each group at each x tick value, with the entire group centered around the x-tick.
It looks a little different in your plot because you have increased the box width.
I don't know of a way to modify individual boxcharts otherwise, and also don't see a way to change the position of a grouped boxchart.
If color is important, you may have better luck using boxplot. See here: https://www.mathworks.com/matlabcentral/answers/1705595-color-each-boxplot-differently
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Axis Labels についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!