Reputation: 3086
I need to add swarmplot
to boxplot
in matplotlib, but I don't know how to do it with factorplot
. I think I can iterate with subplots, but I would like to learn how to do it with seaborn and factorplot.
A simple example (plotting by using the same axis 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")
In my case, I need to overlay the swarm factorplot:
g = sns.factorplot(x="sex", y="total_bill",
hue="smoker", col="time",
data=tips, kind="swarm",
size=4, aspect=.7);
and boxplot
I can't figure out how to use axes (extract from g
)?
Something like:
g = sns.factorplot(x="sex", y="total_bill",
hue="smoker", col="time",
data=tips, kind="box",
size=4, aspect=.7);
I want something like this, but with factorplot
and boxplot
instead of violinplot
Upvotes: 2
Views: 1905
Reputation: 339570
Instead of trying to overlay the two subplots of a factorplot with individual boxplots (which is possible, but I don't like it), one can just create the two subplots individually.
You would then loop over the groups and axes an plot a pair of box- and swarmplot to each.
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()
Upvotes: 4