Extracting Num Points from each box using boxchart
2 次查看(过去 30 天)
显示 更早的评论
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

0 个评论
回答(1 个)
Adam Danz
2022-3-14
编辑:Adam Danz
2022-3-14
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')
groupID = unique(groups)
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 个评论
Adam Danz
2022-3-15
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])
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!