我需要在matplotlib中将swarmplot添加到boxplot,但我不知道如何使用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);和盒图
我不知道如何使用axes (从g中提取)?
类似于:
g = sns.factorplot(x="sex", y="total_bill",
hue="smoker", col="time",
data=tips, kind="box",
size=4, aspect=.7);

我想要这样的东西,但是用factorplot和boxplot代替violinplot

发布于 2018-04-02 13:50:02
我们可以单独创建两个子图,而不是试图将因子图的两个子图与单独的方框图(这是可能的,但我不喜欢)覆盖起来。
然后,您将遍历组并将一幅图轴为一对框,并对每个组进行分组绘制。
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()

https://stackoverflow.com/questions/49612037
复制相似问题