Extracting Num Points from each box using boxchart

2 ビュー (過去 30 日間)
Ali Borhan-Azad
Ali Borhan-Azad 2022 年 3 月 14 日
コメント済み: Ali Borhan-Azad 2022 年 3 月 15 日
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

回答 (1 件)

Adam Danz
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')
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 件のコメント
Adam Danz
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])
Ali Borhan-Azad
Ali Borhan-Azad 2022 年 3 月 15 日
Great, thanks. I'll have a look :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by