I want to plot seaborn boxplot with box from min to max, rather than from 2nd to 3rd quartile, can I control it in matplotlib or seaborn? I know I can control the whiskers - how about boxes?
0
votes
2 Answers
2
votes
Here is an approach that mimics seaborn's boxplot via a horiontal plot using an aggregated dataframe.
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# set sns plot style
sns.set()
tips = sns.load_dataset('tips')
fig, (ax1, ax2) = plt.subplots(nrows=2)
sns.boxplot(x='total_bill', y='day', data=tips, ax=ax1)
day_min_max = tips[['day', 'total_bill']].groupby('day').agg(['min', 'max', 'median'])
day_min_max.columns = day_min_max.columns.droplevel(0) # remove the old column name, only leaving 'min' and 'max'
ax2.use_sticky_edges = False
sns.barplot(y=day_min_max.index, x=day_min_max['median'] - day_min_max['min'], left=day_min_max['min'], ec='k', ax=ax2)
sns.barplot(y=day_min_max.index, x=day_min_max['max'] - day_min_max['median'], left=day_min_max['median'], ec='k', ax=ax2)
plt.tight_layout()
plt.show()
1
votes
Depicting the first and third quartiles is the defining characteristic of a boxplot, so I don't think that this option exists. However, if you want to use the minima and maxima, you are not going to plot any whiskers, and hence you can simply use a barplot instead:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
data = np.random.rand(10, 3)
sns.barplot(x=np.arange(10), y=data.ptp(axis=1), bottom=data.min(axis=1))
plt.show()

