使用 col、row 和 hue 防止 FacetGrid 中的重叠

Prevent Overlapping in FacetGrid using col, row, and hue

我有一个看起来不错的情节

import seaborn as sns
tips = sns.load_dataset('tips')

sns.violinplot('day', 'total_bill', data=tips, hue='sex')

但是,当我想使用 FacetGrid 对象创建一个方面时, 在这个例子中,小提琴被绘制在彼此之上。 我如何防止 tha 发生,使男性和女性彼此相邻绘制?

facet = sns.FacetGrid(tips, col='time', row='smoker', hue='sex',
                 hue_kws={'Male':'blue', 'Female':'green'}).
facet.map(sns.violinplot, 'day', 'total_bill')

似乎解决方案是:

import seaborn as sns
facet = sns.FacetGrid(tips, col="time", row='smoker')
facet.map(sns.violinplot, 'day', 'total_bill', "sex")

sex 传递给 map 调用似乎可以满足我的要求。 但是 sex 分配给的参数名称是什么? 这不是 hue。有人知道这里实际传递的是什么吗?

另一种方法是从 matplotlib.pyplot 级别

准系统开始
import matplotlib.pyplot as plt
import seaborn as sns

facet_fig = plt.figure()
ax1 = facet_fig.add_subplot(2, 2, 1)
ax2 = facet_fig.add_subplot(2, 2, 2)
ax3 = facet_fig.add_subplot(2, 2, 3)
ax4 = facet_fig.add_subplot(2, 2, 4)    

sns.violinplot(x='day', y='total_bill', hue='sex', ax=ax1,
               data=tips[(tips.smoker=='Yes') & (tips.time == 'Lunch')])
sns.violinplot(x='day', y='total_bill', hue='sex', ax=ax2,
               data=tips[(tips.smoker=='Yes') & (tips.time == 'Dinner')])
sns.violinplot(x='day', y='total_bill', hue='sex', ax=ax3,
               data=tips[(tips.smoker=='No') & (tips.time == 'Lunch')])
sns.violinplot(x='day', y='total_bill', hue='sex', ax=ax4,
               data=tips[(tips.smoker=='No') & (tips.time == 'Dinner')])

@mwaskom 提出的更好的解决方案是使用factorplot

sns.factorplot(x='day', y='total_bill', hue='sex', data=tips,
               row='smoker', col='time', kind='violin')