Syed Arefinul Haque
Syed Arefinul Haque

Reputation: 1325

How to change the length of the cap of a whisker in matplotlib boxplot

I am trying to change the length of the caps (the min and max point) of the boxplot whisker which are marked red in the following picture enter image description here

Is it possible to change the length of the min marker and max marker of the whisker without changing the size of the box?

Edit: I meant the increase of the length of the line marker that indicates the min and max end of the whisker, not increasing the length of the whole whisker itself by increasing the confidence interval. In the latest updated pic I show that I want the black min and max marker to be increased so that it matches the size I indicated with red line.

Upvotes: 5

Views: 5079

Answers (3)

fig, axes = plt.subplots(nrows = 1, ncols = 2, figsize=(10, 5))

normal_caps = axes[0].boxplot(s, labels = ['Normal Caps'],
                capprops = dict(linestyle='-', linewidth=2, color='Black'))

big_caps = axes[1].boxplot(s, labels = ['Longer Caps'],
                capprops = dict(linestyle='-', linewidth=2, color='Black'))

for cap in big_caps['caps']:
    cap.set_xdata(cap.get_xdata() + np.array([-.15,.15]))

Upvotes: 2

wwii
wwii

Reputation: 23753

Some fake data straight from a boxplot example

# fake up some more data
spread = np.random.rand(50) * 100
center = np.ones(25) * 40
flier_high = np.random.rand(10) * 100 + 100
flier_low = np.random.rand(10) * -100
d2 = np.concatenate((spread, center, flier_high, flier_low), 0)
data.shape = (-1, 1)
d2.shape = (-1, 1)
# data = concatenate( (data, d2), 1 )
# Making a 2-D array only works if all the columns are the
# same length.  If they are not, then use a list instead.
# This is actually more efficient because boxplot converts
# a 2-D array into a list of vectors internally anyway.
data = [data, d2, d2[::2, 0]]
# multiple box plots on one figure

pyplot.boxplot returns a dictionary of Line2D instances, the caps are what you want to change. This solution will make them longer by .5 x-axis units, set their colors, and linewidths.

plt.figure()
returns = plt.boxplot(data, 0, '')

caps = returns['caps']
n = .25
n = .25
for cap, color in zip(caps, ['xkcd:azul','aquamarine','crimson','darkorchid','coral','thistle']):
    #print(cap.properties()['xdata'])
    #cap.set_xdata(cap.get_xdata() + (-n,+n))
    #cap.set_color(color)
    #cap.set_linewidth(4.0)
    cap.set(color=color, xdata=cap.get_xdata() + (-n,+n), linewidth=4.0)


Artist Tutorial

Upvotes: 3

Jessica Resnick
Jessica Resnick

Reputation: 109

It is possible by adding the argument whis when you create your box plot

matplotlib.axes.Axes.boxplot

whis : float, sequence, or string (default = 1.5)
As a float, determines the reach of the whiskers to the beyond the first and  
third quartiles. In other words, where IQR is the interquartile range (Q3-Q1), 
the upper whisker will extend to last datum less than Q3 + whis*IQR). 
Similarly, the lower whisker will extend to the first datum greater than Q1 - 
whis*IQR. Beyond the whiskers, data are considered outliers and are plotted as 
individual points. Set this to an unreasonably high value to force the whiskers 
to show the min and max values. Alternatively, set this to an ascending 
sequence of percentile (e.g., [5, 95]) to set the whiskers at specific 
percentiles of the data. Finally, whis can be the string 'range' to force the
whiskers to the min and max of the data.

Upvotes: -2

Related Questions