How to get position of single box in grouped box chart on x-axis?
20 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Scott MacKenzie
on 11 May 2021
Edited: Scott MacKenzie
on 11 May 2021
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');
0 Comments
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!