![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/614570/image.jpeg)
How to get position of single box in grouped box chart on x-axis?
22 次查看(过去 30 天)
显示 更早的评论
I would like to get the position of each single box on the x-axis I created with boxchart(). But the box chart object is grouped, so when I type XAxis.Categories I only get the two grouping categories even though it's actually 6 boxes.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/521202/image.png)
0 个评论
回答(1 个)
Scott MacKenzie
2021-5-11
编辑:Scott MacKenzie
2021-5-11
The use of grouped data changes the XData type to categorical for the BoxChart. Plus, there doesn't seem to be any BoxChart property that gives the x position of the group or the x-offset of the boxes within a group. I determined that the actual x positions of the groups are 1, 2, 3, etc., as they would be if the data were not categorical. Knowing this, I concocted a scheme to compute the x-position of each box, so that text annotations (or whatever) can be added. See below. This can probably be vectorized in some way, but I thought it might useful to post this in the present form.
set(groot, 'defaultTextHorizontalAlignment', 'center');
m = 3; % number of boxes per group
xGap = 1 / m;
grp = 1; % x position for 1st group
text(grp-xGap, 30, 'a1');
text(grp, 30, 'a2');
text(grp+xGap, 30, 'a3');
grp = 2; % x position for 2nd group
text(grp-xGap, 30, 'b1');
text(grp, 30, 'b2');
text(grp+xGap, 30, 'b3');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/614570/image.jpeg)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Properties 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!