Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing what the ends of whiskers represent in matplotlib's boxplot function

I understand that the ends of whiskers in matplotlib's box plot function extend to max value below 75% + 1.5 IQR and minimum value above 25% - 1.5 IQR. I would like to change it to represent max and minimum values of the data or the 5th and 95th quartile of the data. Is is possible to do this?

like image 404
JayKu12 Avatar asked Feb 24 '14 20:02

JayKu12


People also ask

Can we customize the whiskers in a Boxplot?

However, boxplot() can only set cap values of whiskers as the values of percentiles. e.g. Given my distribution is not a normal distribution, then the 95th/5th percentiles will not be the (mean+2std)/(mean-2std).

How do you change Boxplot whiskers?

4, you can say: boxplots = ax. boxplot(myData, whis=[5, 95]) to set the whiskers at the 5th and 95th percentiles. Similarly, you'll be able to say boxplots = ax. boxplot(myData, whis=[0, 100]) to set the whiskers at the min and max.

What are the endpoints of whiskers in a box plot?

The whiskers are the two lines outside the box, that go from the minimum to the lower quartile (the start of the box) and then from the upper quartile (the end of the box) to the maximum.

What does the ends of whiskers show in a box-and-whisker plot?

At the ends of the box, you” find the first quartile (the 25% mark) and the third quartile (the 75% mark). The far left of the chart (at the end of the left “whisker”) is the minimum (the smallest number in the set) and the far right is the maximum (the largest number in the set).


2 Answers

To get the whiskers to appear at the min and max of the data, set the whis parameter to an arbitrarily large number. In other words: boxplots = ax.boxplot(myData, whis=np.inf).

The whis kwarg is a scaling factor of the interquartile range. Whiskers are drawn to the outermost data points within whis * IQR away from the quartiles.

Now that v1.4 is out:

In matplotlib v1.4, you can say: boxplots = ax.boxplot(myData, whis=[5, 95]) to set the whiskers at the 5th and 95th percentiles. Similarly, you'll be able to say boxplots = ax.boxplot(myData, whis='range') to set the whiskers at the min and max.

Note: you could probably modify the artists contained in the boxplots dictionary returned by the ax.boxplot method, but that seems like a huge hassle

like image 161
Paul H Avatar answered Oct 05 '22 03:10

Paul H


Set the boxplot option whisk=0 to hide the inbuilt whiskers. Then create the custom whiskers that show data from 5% to 95%.

#create markings that represent the ends of whiskers
low=data.quantile(0.05) 
high=data.quantile(0.95)
plt.scatter(range(1,len(low)+1),low,marker='_')
plt.scatter(range(1,len(low)+1),high,marker='_')
#connects low and high markers with a line
plt.vlines(range(1,len(low)+1),low,high)

That should create vertical lines with whisker markings behind the boxes at 5% at 95%.

like image 39
user3582605 Avatar answered Oct 05 '22 04:10

user3582605