2

我需要在 matplotlib 中添加swarmplotboxplot但我不知道如何使用factorplot. 我想我可以用子图进行迭代,但我想学习如何用 seaborn 和 factorplot 来做。

一个简单的例子(使用相同的轴绘制ax):

import seaborn as sns
tips = sns.load_dataset("tips")
ax = sns.boxplot(x="tip", y="day", data=tips, whis=np.inf)
ax = sns.swarmplot(x="tip", y="day", data=tips, color=".2")

结果:在此处输入图像描述

就我而言,我需要覆盖群因子图:

g = sns.factorplot(x="sex", y="total_bill",
                      hue="smoker", col="time",
                      data=tips, kind="swarm",
                      size=4, aspect=.7);

箱线图

我不知道如何使用轴(从 中提取g)?

就像是:

g = sns.factorplot(x="sex", y="total_bill",
                          hue="smoker", col="time",
                          data=tips, kind="box",
                          size=4, aspect=.7);

在此处输入图像描述

我想要这样的东西,但是用factorplotandboxplot而不是violinplot

在此处输入图像描述

4

1 回答 1

4

与其尝试用单独的箱线图覆盖因子图的两个子图(这是可能的,但我不喜欢它),不如单独创建两个子图。

然后,您将遍历组和轴,并为每个绘制一对 box- 和 swarmplot。

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

tips = sns.load_dataset("tips")

fig, axes = plt.subplots(ncols=2, sharex=True, sharey=True)

for ax, (n,grp) in zip(axes, tips.groupby("time")):
    sns.boxplot(x="sex", y="total_bill", data=grp, whis=np.inf, ax=ax)
    sns.swarmplot(x="sex", y="total_bill", hue="smoker", data=grp, 
                  palette=["crimson","indigo"], ax=ax)
    ax.set_title(n)
axes[-1].get_legend().remove()
plt.show()

在此处输入图像描述

于 2018-04-02T13:50:02.657 回答