You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现左右分属A/B类别且各带独立box、whiskers及四分位距的拼接Violin plot

如何实现左右分属A/B类别且各带独立box、whiskers及四分位距的拼接Violin plot

嘿,我完全懂你的需求啦——你想在同一个x轴分组(比如你的thurs)下,左边展示类别A的小提琴分布,右边展示类别B的,而且两边各自带独立的箱线、须线和四分位区间,而不是共用中间那一个箱线对吧?

问题出在split=True这个参数上:当你用它的时候,seaborn会把两个类别的小提琴拼在一起,但默认会把箱线合并成中间的一个,因为它默认认为这两个类别是同一组的“左右拆分”,而非独立的两组。要实现你想要的效果,我们可以手动分开绘制左右两边的小提琴和对应的箱线图,具体做法如下:

首先,我们需要分别针对类别A和B的数据绘制左半、右半小提琴,再各自叠加对应的箱线图:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# 你的示例数据
df1 = pd.DataFrame({
    'category': ['A', 'A', 'B', 'B', 'B'],
    'Value': [10, 200, 300, 40, 50]
})

df2 = pd.DataFrame({
    'category': ['A', 'A', 'B', 'B', 'B'],
    'Value': [3, 4, 5, 4, 1]
})

df1['day'] = 'thurs'
df2['day'] = 'thurs'
combined_df = pd.concat([df1, df2])

plt.figure(figsize=(10, 6))

# 分别绘制左半(A类)和右半(B类)的小提琴
sns.violinplot(
    x='day', y='Value', data=combined_df[combined_df['category'] == 'A'],
    side='left', width=0.4, color='#66b3ff', label='A'
)
sns.violinplot(
    x='day', y='Value', data=combined_df[combined_df['category'] == 'B'],
    side='right', width=0.4, color='#ff9999', label='B'
)

# 分别绘制左右两边的箱线图,位置微调以对应小提琴
sns.boxplot(
    x='day', y='Value', data=combined_df[combined_df['category'] == 'A'],
    width=0.1, boxprops={'zorder': 2}, color='white', positions=[-0.2]
)
sns.boxplot(
    x='day', y='Value', data=combined_df[combined_df['category'] == 'B'],
    width=0.1, boxprops={'zorder': 2}, color='white', positions=[0.2]
)

plt.title('Violin Plot with Separate Boxes for Category A and B')
plt.legend(title='Category')
plt.show()

几个关键细节的解释:

  • 用side='left'和side='right'分别指定小提琴只显示左半或右半部分,这样就能拼接成一个完整的“组合小提琴”;
  • 箱线图需要手动调整positions参数,把A的箱线放在x轴位置-0.2,B的放在0.2,刚好对应左右小提琴的位置;zorder=2是为了让箱线显示在小提琴上方,避免被遮挡;width=0.1让箱线更细,符合拼接小提琴的视觉效果;
  • 用不同颜色区分两个类别,让可视化的信息层级更清晰。

这样做出来的图就会和你想要的效果一致:同一个x分组下,左右分别是A和B的分布,各自带有独立的箱线、须线和四分位区间啦!

备注:内容来源于stack exchange,提问作者eshaa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 15:38:07