Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the linestyle of whiskers in pandas boxplots?

Is there a way to change the linestyle of the whiskers in pandas boxplots to '-'? Default seems to be '--'.

I have tried:

color = dict(boxes='black', whiskers='black', medians='red', caps='black')
styles=dict(whiskers='-')
bp = df.plot.box(color=color, style=styles)

However, while the colors turn out the way I want, the style input does not seem to affect the plot at all.

Here is an example. I always get dashed lines for my whiskers, but would like solid lines.

I have also tried

boxprops = dict(linewidth=1.0, color='black')
whiskerprops = dict(linestyle='-',linewidth=1.0, color='black')
plt.figure()
df.boxplot(boxprops=boxprops, whiskerprops=whiskerprops)

Here, df.boxplot does not take the inputs at all.

This is closely related to Pandas boxplot: set color and properties for box, median, mean

like image 631
knut_h Avatar asked Sep 14 '17 18:09

knut_h


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 the axis of a boxplot in Python?

Using boxplot(), draw a box plot to show distributions with respect to categories. To set the range of Y-axis, use the ylim() method. To display the figure, use the show() method.

Which parameter is used to change the position of Boxplots?

conf_intervals : This parameter is also an array or sequence whose first dimension is compatible with x and whose second dimension is 2. positions : This parameter is used to sets the positions of the boxes.


2 Answers

Ted Petrou's commments helped:

Put the whiskerprops = dict() directly in to the df.plot.box line:

color = dict(boxes='black', whiskers='black', medians='red', caps='black')
bp = df.plot.box(color=color,whiskerprops = dict(linestyle='-',linewidth=1.0
, color='black'))

As for df.boxplot(), there seems to be a problem with byarguments. Including whiskerprops and boxprops directly into the argument, here, helped as well. However I could still not change the boxes' color! It remains to be the default blue. The following code yields solid-line, black whiskers, however the boxes are blue. Linewidth of boxes can be changed tho!

plt.figure()
df.boxplot(boxprops= dict(linewidth=1.0, color='black')
, whiskerprops=dict(linestyle='-',linewidth=1.0, color='black'))

If anyone can help with changing boxes colors in df.boxplot(), please do comment. From the pandas documentation I get, that people should rather use df.plot.box anyways tho.

like image 194
knut_h Avatar answered Sep 19 '22 00:09

knut_h


import numpy as np
import pandas as pd

mu, sigma = 0, 1 
s = np.random.normal(mu, sigma, 1000)

df = pd.DataFrame(s)

bPlot = df.boxplot(whiskerprops = dict(linestyle='--'
                           , linewidth=2))

enter image description here

like image 37
Michael James Kali Galarnyk Avatar answered Sep 19 '22 00:09

Michael James Kali Galarnyk