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 CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by