将框宽从 IQR 更改为用户在 seaborn 箱线图中定义

Change box width from IQR to user defined in seaborn boxplot

我想绘制 seaborn 箱线图,箱形图从最小值到最大值,而不是从第 2 到第 3 个四分位数,我可以在 matplotlib 或 seaborn 中控制它吗?我知道我可以控制胡须 - 盒子怎么样?

描绘第一和第三四分位数是箱线图的定义特征,因此我认为不存在此选项。但是,如果您想使用最小值和最大值,则不会绘制任何须线,因此您可以简单地使用条形图来代替:

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()

这是一种使用聚合数据框通过水平图模拟 seaborn 箱线图的方法。

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()