Black and white boxplots in Seaborn
I was just exploring this and it seems there is another way to do this now. Basically, there are the keywords boxprops
, medianprops
, whiskerprops
and (you guessed it) capprops
, all of which are dictionaries that may be passed to the boxplot func. I choose to define them above and then unpack them for readability:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
_to_plot = pd.DataFrame(
{
0: np.random.normal(0,1,100),
1: np.random.normal(0,2,100),
2: np.random.normal(0,-1,100),
3: np.random.normal(0,-2,100)
}
).melt()
PROPS = {
'boxprops':{'facecolor':'none', 'edgecolor':'red'},
'medianprops':{'color':'green'},
'whiskerprops':{'color':'blue'},
'capprops':{'color':'yellow'}
}
sns.boxplot(x='variable',y='value',
data=_to_plot,
showfliers=False,
linewidth=0.75,
**PROPS)
You have to set edgecolor
of every boxes and the use set_color
for six lines (whiskers and median) associated with every box:
ax = sns.boxplot(x="day", y="total_bill", data=tips, color='white', width=.5, fliersize=0)
# iterate over boxes
for i,box in enumerate(ax.artists):
box.set_edgecolor('black')
box.set_facecolor('white')
# iterate over whiskers and median lines
for j in range(6*i,6*(i+1)):
ax.lines[j].set_color('black')
If last cycle is applied for all artists and lines then it may be reduced to:
plt.setp(ax.artists, edgecolor = 'k', facecolor='w')
plt.setp(ax.lines, color='k')
where ax
according to boxplot
.
If you also need to set fliers' color follow this answer.