我正在尝试用一个seaborn.facetgrid
替换一个seaborn.catplot
。但是seaborn.catplot
没有在Embarked = C
方面正确地标注色调。
数据集:泰坦尼克号
e = sns.FacetGrid(data= train_df, col='Embarked')
e.map_dataframe(sns.pointplot, 'Pclass', 'Survived', hue='Sex', palette='deep')
e.add_legend()
登船C:男性被正确地呈现为色调
但我的seaborn.catplot
显示:
sns.catplot(x='Pclass', y= 'Survived', hue='Sex', data=train_df, kind='point', col='Embarked')
登船C:男性没有正确地表示为色调
发布于 2021-06-16 03:00:55
JohanC已经在他的评论中取笑了这个答案。我只会解释和完整。
以下是文档 of seaborn.catplot
对订购的看法:
与底层绘图函数的情况一样,如果变量具有分类数据类型,则将从对象推断出分类变量的级别及其顺序。否则,您可能必须使用更改数据排序或使用函数参数(
orient
、order
、hue_order
等)。正确设置情节。
这意味着您可以使用hue_order
参数来确保按您希望的顺序排列绘图:
order,hue_order:字符串列表,可选的 为了在其中绘制分类级别,否则将从数据对象推断级别。
在这里,如何在您的情况下使用它:
sns.catplot(x='Pclass', y='Survived', hue='Sex', hue_order=['male', 'female'], data=train_df, kind='point', col='Embarked')
或者,正如文档中所描述并由JohanC指出的,您可以将列train_df['Sex']
的类型转换为绝对类型。然后,顺序将由seaborn
推断。
发布于 2021-06-16 20:16:46
谢谢JohanC。是的,我的FacetGrid情节是错的。我手动检查过了。
train_df[(train_df['Embarked']=='C') & (train_df['Survived']==1)].groupby('Sex').count()['Survived']
产出:
Sex
female 64
male 29
女性比男性大。在FacetGrid
中,应该指定hue_order
,否则可能会产生错误的结果。
https://stackoverflow.com/questions/67996295
复制相似问题