Seaborn如何在sns.catplot中添加每个色调的样本数

2024-03-29 07:53:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个catplot图纸,使用:

s = sns.catplot(x="type", y="val", hue="Condition", kind='box', data=df)

enter image description here

但是,每个色调的“条件”大小不相等: 蓝色有n=8个样本,绿色有n=11个样本

将此信息添加到图表中的最佳方式是什么


Tags: boxdfdatatypevalcondition条件色调
1条回答
网友
1楼 · 发布于 2024-03-29 07:53:35

这基本上是与an earlier answer of mine相同的解决方案,我简化了一点,因为:

df = sns.load_dataset('tips')
x_col='day'
y_col='total_bill'
order=['Thur','Fri','Sat','Sun']
hue_col='smoker'
hue_order=['Yes','No']
width=0.8


g = sns.catplot(kind="box", x=x_col, y=y_col, order=order, hue=hue_col, hue_order=hue_order, data=df)
ax = g.axes[0,0]

# get the offsets used by boxplot when hue-nesting is used
# https://github.com/mwaskom/seaborn/blob/c73055b2a9d9830c6fbbace07127c370389d04dd/seaborn/categorical.py#L367
n_levels = len(df[hue_col].unique())
each_width = width / n_levels
offsets = np.linspace(0, width - each_width, n_levels)
offsets -= offsets.mean()

pos = [x+o for x in np.arange(len(order)) for o in offsets]

counts = df.groupby([x_col,hue_col])[y_col].size()
counts = counts.reindex(pd.MultiIndex.from_product([order,hue_order]))
medians = df.groupby([x_col,hue_col])[y_col].median()
medians = medians.reindex(pd.MultiIndex.from_product([order,hue_order]))

for p,n,m in zip(pos,counts,medians):
    if not np.isnan(m):
        ax.annotate('N={:.0f}'.format(n), xy=(p, m), xycoords='data', ha='center', va='bottom')

enter image description here

相关问题 更多 >