Whiskers in Boxplot seems incorrect

22 views (last 30 days)
I am having a problem with getting the whiskers in the boxplot correct. I have cooked my code down to the below. I compute the upper whisker value to 99.54, but the boxplot show this as around 50.
%Example BoxPlot
x=[126.0 32.2 19.4 0.02 -1.4 272.75 26.0 -0.01 3.66 40.86 16.7 1.8 ...
16.0 462.1 13.2 45.4 23.0 166.4 11.9 14.96 14.20 30.20 1.7 0.9 0.2];
q75_test=quantile(x,0.75);
q25_test=quantile(x,0.25);
w=2.0;
wUpper=w*(q75_test-q25_test)+q75_test;
boxplot(x,'whisker',w);
line([0 7],[wUpper wUpper]);
BoxplotWhisker.jpg

Accepted Answer

dpb
dpb on 4 May 2019
Edited: dpb on 21 Aug 2019
Read the description of 'whisker' carefully...particularly the very last sentence below (emphasis mine):
"boxplot draws points as outliers if they are greater than q3 + w × (q3 – q1) or less than q1 – w × (q3 – q1), where w is the maximum whisker length, and q1 and q3 are the 25th and 75th percentiles of the sample data, respectively.
The default value for 'Whisker' corresponds to approximately +/–2.7σ and 99.3 percent coverage if the data are normally distributed. The plotted whisker extends to the adjacent value, which is the most extreme data value that is not an outlier."
The first point plotted as outlier is >100 which matches up with your wUpper computed value and the whisker extends to
>> xs=sort(x); % sort to find wUpper location
>> xs(find(xs>=wUpper,1)-1) % and first below it is...drum roll, please!!!
ans =
45.4000
>> hold on
>> plot(1,ans,'r*') % right where the whisker is..
>>
So, the plot is precisely what is described...the whisker indicates the actual location of the last non-outlier, not the boundary value that in general doesn't match any observation.

More Answers (1)

Jon Duell
Jon Duell on 20 Jun 2021
Edited: Jon Duell on 20 Jun 2021
I'm certain a lot of people would appreciate functionality which would extend the whisker to the statistical definition, not nearest adjacent. I'm heading through the in-built function to see if I can manage it, learning a lot, it's an impressive bit of code for me.
All the best
J Duell

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!