MATLAB Answers

Whiskers in Boxplot seems incorrect

74 views (last 30 days)
Niels Jorgensen
Niels Jorgensen on 4 May 2019
Edited: dpb on 21 Aug 2019
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

  0 Comments

Sign in to comment.

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.

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Products


Release

R2017a