gpt4 book ai didi

python - 如何使用 seaborn 在同一张图上绘制两个 fiddle 图系列?

转载 作者:太空狗 更新时间:2023-10-30 01:05:07 25 4
gpt4 key购买 nike

查看有关 violon plots 的文档使用 seaborn,我想知道如何在同一轴上绘制两个系列的 fiddle 图(第 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 轴上绘制两个 fiddle 情节系列。

还有一点是关于 fiddle 情节的宽度。我不清楚 fiddle 是否标准化以及如何标准化?我假设为每个图计算宽度。在上面的示例中,第一个子图的 Female 和第二个子图的 Male 计算了宽度。因此我可以直接比较密度吗?我想我可以比较形状,但是,例如,我无法比较星期一男性吸烟者和女性吸烟者的数量?有没有办法管理 fiddle 的正常化?

最佳答案

对于你的第一点,在 Seaborn 中无法做到这一点。查看我的评论以了解可能的解决方法,但简而言之,我认为花费的时间不值得。

对于你的第二个问题,violinplotscalescale_hue 参数控制 fiddle 补丁的标准化/缩放方式:

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。您可以在下面看到这些参数的变化如何影响 fiddle 。例如,如果您想要比较绘图并如实表示绝对计数,您可以设置 scale='count'scale_hue=False。请注意, fiddle 仍将按比例缩放到图中(而不是数据集中)的最大数量,因此在我们的例子中,女性最大的 fiddle 将代表约 40 个观察结果,而男性最大的 fiddle 将代表约 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()

enter image description here

添加 scale_hue=False: enter image description here

关于python - 如何使用 seaborn 在同一张图上绘制两个 fiddle 图系列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47409086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com