使用col、row和hue避免FacetGrid重叠

3
我有一个看起来不错的情节。
import seaborn as sns
tips = sns.load_dataset('tips')

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

在此输入图片描述

然而,当我使用FacetGrid对象创建分面时, 在这个例子中,提琴图是重叠绘制的。 如何防止这种情况发生,以便男性和女性并排绘制?

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

enter image description here


我没有看到从我的电脑添加图形的选项 :x我已经更新了所有带有图片的帖子。 - Daniel Chen
@mwaskom:谢谢,看起来sns.factorplot(x='day', y='total_bill', hue='sex', data=tips, row='smoker', col='time', kind='violin')解决了问题。 - Daniel Chen
2个回答

3
@mwaskom提出的更好的解决方案是使用factorplot
sns.factorplot(x='day', y='total_bill', hue='sex', data=tips,
               row='smoker', col='time', kind='violin')

enter image description here


2
多个答案和图片。继续加油! :) - Russia Must Remove Putin

2

看起来解决方案是:

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')])

enter image description here


实际上,我想通了。map将采用位置参数,从sns.violinplot参数的顺序是xy,然后是hue。因此,sex被传递到hue,只是不作为关键字参数工作。 - Daniel Chen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接