Gnesson
Gnesson

Reputation: 31

How to change percentile values of for the built in Matlab boxplot function?

I have an issue with the use of Matlab R2010b and the boxplot function.

With a previous version of Matlab, I did some modifications in boxplot.m file so that I could change the percentile values used. By default, boxplots are built considering the first and third quartile (25th and 75th percentiles) to define whiskers. My interest is to use 10th and 90th percentiles.

I tried every solutions I found on the Internet.

So my question is: Has anyone found a way to change the default values (25th and 75th) of percentiles used by the boxplot function of Matlab (R2010b and after)?

Many many thanks!

Upvotes: 3

Views: 7624

Answers (1)

marsei
marsei

Reputation: 7751

You can change the way boxplot display the data/quantiles by modifying the properties of the graphical object (and not modifying the function per se).

Here is a piece of code that will modify the quantiles used for the blue box (initially, the blue box corresponds to the .25 and .75 quantiles, and will change to .1 and .9). The base part of the upper/lower whisker will change accordingly. Please note that the tips of the whiskers are unchanged (they still correspond to 1.5 of the interquartile range). You can change the tips of the whiskers just like we changed their base parts.

%%% load some data  
load carsmall  
MPG  = MPG(ismember(Origin,'USA','rows'));  
Origin = Origin(ismember(Origin,'USA','rows'),:)  
Origin(isnan(MPG),:) = [];  
MPG   (isnan(MPG),:) = [];  

%%% quantile calculation  
q = quantile(MPG,[0.1 0.25 0.75 0.9]);  
q10 = q(1);  
q25 = q(2);  
q75 = q(3);  
q90 = q(4);  

%%% boxplot the data  
figure('Color','w');  

subplot(1,2,1);  
boxplot(MPG,Origin);   
title('original boxplot with quartile', 'FontSize', 14, 'FontWeight', 'b', 'Color', 'r');  
set(gca, 'FontSize', 14);  

subplot(1,2,2);  
h = boxplot(MPG,Origin) %define the handles of boxplot  
title('modified boxplot with [.1 .9] quantiles', 'FontSize', 14, 'FontWeight', 'b', 'Color', 'r');  
set(gca, 'FontSize', 14);  

%%% modify the figure properties (set the YData property)  
%h(5,1) correspond the blue box  
%h(1,1) correspond the upper whisker  
%h(2,1) correspond the lower whisker  
set(h(5,1), 'YData', [q10 q90 q90 q10 q10]);% blue box  

upWhisker = get(h(1,1), 'YData');  
set(h(1,1), 'YData', [q90 upWhisker(2)])  

dwWhisker = get(h(2,1), 'YData');  
set(h(2,1), 'YData', [ dwWhisker(1) q10])  


%%% all of the boxplot properties are here  
for ii = 1:7  
   ii  
   get(h(ii,1))  
end  

And here are the results.

enter image description here

Upvotes: 1

Related Questions