Extracting Num Points from each box using boxchart

2 views (last 30 days)
Hi,
I am using boxchart to create various box plots. When viewing the figure, there is a property for each box called Num Points, as shown in the image. Is there any way to extract this value?
Thanks in advance

Answers (1)

Adam Danz
Adam Danz on 14 Mar 2022
Edited: Adam Danz on 14 Mar 2022
How to get Num Points....
...From the data
The most efficient way to get the number of points that compose each box is from the data itself. Using the syntax boxchart(ydata), the number of points in each bin is the height of ydata. Using the syntax boxplot(xgroupdata,ydata), the number of points in each bin is the number of times each group value appears in xgroupdata. For example, using the data below, there are 9 points in group 1 and 16 points in group 9.
rng('default') % for reproducibility
groups = randi(9,1,100); % groups 1:9
y = rand(size(groups)); % random y data
% boxchart(groups,y)
groupCount = histcounts(groups,'BinMethod','integers')
groupCount = 1×9
9 11 11 9 11 10 13 10 16
groupID = unique(groups)
groupID = 1×9
1 2 3 4 5 6 7 8 9
If your group values are not integers, you can use findgroups to convert them to integer values.
...From the datatip
If this is something you only need to do a few times and you do not need a programmatic approach, method #3 in this answer is easiest. Alternatively, you could use getDataTip from the file exchange. It extracts the datatip content and handles from a figure, axis, or an array of figures and axes in Matlab r2014 and later. A third alternative off the top of my head is to create a custom update function so that the NumPoints is extracted when the user presses the box.
  4 Comments
Adam Danz
Adam Danz on 15 Mar 2022
Well done with finding a solution to your follow-up question! You could also look into findgroups, perhaps something like,
[G,GID] = findgroups([xgroups, subcat])

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!