如何使用seaborn在同一张图上绘制两个小提琴图系列?

查看有关seaborn的violon plots的文档,我想知道如何在同一轴上绘制两个小提琴图系列(点1)并且它们是可比较的(点2).

关于第1点,我想复制每种性别的情节:

fig, ax = plt.subplots()
sns.violinplot(x="day", y="total_bill", hue="smoker",
                    data=tips, split=True, ax=ax)

我可以在两个子图上做到这一点:

fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(211)
sns.violinplot(x="day", y="total_bill", hue="smoker",
               data=tips[tips.sex == "Female"], split=True, ax=ax)

ax = fig.add_subplot(212)
sns.violinplot(x="day", y="total_bill", hue="smoker",
               data=tips[tips.sex == "Male"], split=True, ax=ax)

我想在相同的matplotlib轴上绘制两个小提琴图系列.

另一点是关于小提琴图的宽度.我不清楚小提琴是否已标准化以及如何标准化?我假设宽度是为每个图计算的.在上面的示例中,宽度是针对第一个子图的“女性”和第二个子图的“男性”计算的.因此,我可以直接比较密度吗?我想我可以比较形状,但是,例如,我不能比较星期一和女性吸烟者的数量.有没有办法管理小提琴的标准化?

解决方法:

对于您的第一点,在Seaborn中无法做到这一点.请查看我的评论以获取可能的解决方法,但总之,我认为花费的时间并不值得.

对于第二个问题,violinplot的scale和scale_hue参数控制小提琴音色的标准化/缩放方式:

scale : {“area”, “count”, “width”}, optional

The method used to scale the width of each violin. If area, each violin will have the same area. If count, the width of the violins will be scaled by the number of observations in that bin. If width, each violin will have the same width.

scale_hue : bool, optional

When nesting violins using a hue variable, this parameter determines whether the scaling is computed within each level of the major grouping variable (scale_hue=True) or across all the violins on the plot (scale_hue=False).

默认值为“ area”和“ False”.您可以在下面看到这些参数的变化如何影响小提琴.例如,如果要在图之间进行比较并如实表示绝对计数,则可以设置scale =’count’和scale_hue = False.请注意,小提琴仍将按比例缩放到图中的最大数量(而不是数据集中),因此在我们的案例中,女性最大的小提琴将代表〜40个观测值,而男性最大的小提琴将代表〜25个观测值.

fig, axes = plt.subplots(4, 2, figsize=(10, 16), sharey='row')
axes_cols = (axes.flatten()[::2], axes.flatten()[1::2])

for (sex_name, sex), axes_col in zip(tips.groupby('sex'), axes_cols):
    sns.countplot(x="day", hue="smoker", data=sex, ax=axes_col[0])
    for scale, ax in zip(['area', 'count', 'width'], axes_col[1:]):
        sns.violinplot(x="day", y="total_bill", hue="smoker",
            data=sex, split=True, ax=ax, scale=scale)
        ax.set_title('scale = {}'.format(scale), y=0.95)
sns.despine()
fig.tight_layout()

如何使用seaborn在同一张图上绘制两个小提琴图系列?

添加scale_hue = False:
如何使用seaborn在同一张图上绘制两个小提琴图系列?

上一篇:在python seaborn图中创建多列图例


下一篇:tkinter,python和seaborn的问题:_tkinter.TclError:没有显示名称,没有$DISPLAY环境变量