要自定义显示在小提琴图内的箱线图的样式,可以尝试在小提琴图前绘制箱线图。然而,这似乎不起作用,因为它在使用 seaborn 时总是显示在小提琴图后面。
当使用 seaborn + matplotlib 时,这有效(但仅适用于单个类别):
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
df=pd.DataFrame(np.random.rand(10,2)).melt(var_name='group')
fig,axes = plt.subplots()
# Seaborn violin plot
sns.violinplot(y=df[df['group']==0]['value'],color="#af52f4",inner=None,linewidth=0,saturation=0.5)
# Normal boxplot has full range,same in Seaborn boxplot
axes.boxplot(df[df['group']==0]['value'],whis='range',positions=np.array([0]),showcaps=False,widths=0.06,patch_artist=True,boxprops=dict(color="indigo",facecolor="indigo"),whiskerprops=dict(color="indigo",linewidth=2),medianprops=dict(color="w",linewidth=2 ))
axes.set_xlim(-1,1)
plt.show()
然而,当只使用 seaborn 来绘制多个类别时,排序总是错误的:
sns.violinplot(data=df,x='group',y='value',saturation=0.5)
sns.boxplot(data=df,saturation=0.5)
plt.show()
即使尝试使用 zorder
解决此问题,这也不起作用。