How to label datapoints in a boxplot?
显示 更早的评论
Hello! I have a boxplot with 3 subplots in it. I want to display each median, outlier, whisker, etc in my figure. I do not want to use a text format because I want to use the same code for different CSV files. The CSV files I will use later on have different medians, outliers, etc.
Hope anyone can help me
Thank yo!
回答(2 个)
Kishan Dhakan
2021-6-30
0 个投票
To modify graphics properties of a box plot component, use findobj with the Tag property to find the component's handle. Tag values for box plot components depend on parameter settings, and are listed in the following table.
Parameter SettingsTag Values
All settings
- 'Box'
- 'Outliers'
When 'PlotStyle' is 'traditional'
- 'Median'
- 'Upper Whisker'
- 'Lower Whisker'
- 'Upper Adjacent Value'
- 'Lower Adjacent Value'
When 'PlotStyle' is 'compact'
- 'Whisker'
- 'MedianOuter'
- 'MedianInner'
When 'Notch' is 'marker'
- 'NotchLo'
- 'NotchHi'
I found a way to brute-force it with the text function. The example below is for the percentiles only. I didn't try labeling outliers.
The variable i is used to call each dataset (I don't know how you organized your data) and also used to place the labels in the first, second, third, etc. columns. I nudge the label a bit (0.1) to right of center of each column, and a bit (5) above each line.
First you need to make your boxplot, and then:
for i = 1:3
switch i
case 1; Data = Thirties;
case 2; Data = Sixties;
case 3; Data = Nineties;
otherwise; Data = [];
end
point = quantile(Data,0.25); text(i+0.1,point+5,num2str(point,'%.0f')); % 25 percentile
point = quantile(Data,0.50); text(i+0.1,point+5,num2str(point,'%.0f')); % median
point = quantile(Data,0.75); text(i+0.1,point+5,num2str(point,'%.0f')); % 75 percentile
point = quantile(Data,0.75)+1.5*iqr(Data); text(i+0.1,point+5,num2str(point,'%.0f')); % upper whisker
point = quantile(Data,0.25)-1.5*iqr(Data); text(i+0.1,point+5,num2str(point,'%.0f')); % lower whisker
end
Good luck to others who stumble on this.
类别
在 帮助中心 和 File Exchange 中查找有关 Exploration and Visualization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

