小编Mon*_*ave的帖子

如何在seaborn中的小提琴图前面显示箱线图 - seaborn zorder?

要自定义小提琴图中显示的箱线图的样式,可以尝试在小提琴图前面绘制箱线图。然而,这似乎不起作用,因为使用 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()
Run Code Online (Sandbox Code Playgroud)

正确放置的箱线图

然而,当仅使用seaborn能够跨多个类别进行绘图时,顺序总是错误的:

sns.violinplot(data=df, x='group', y='value', color="#af52f4", inner=None, linewidth=0, saturation=0.5)
sns.boxplot(data=df, x='group', y='value', saturation=0.5)


plt.show()

Run Code Online (Sandbox Code Playgroud)

箱线图放置不正确

即使尝试用此方法修复此zorder问题也不起作用。

python matplotlib seaborn

4
推荐指数
1
解决办法
4331
查看次数

标签 统计

matplotlib ×1

python ×1

seaborn ×1