问题描述
以下是kaggle泰坦尼克号比赛的代码内核:
Here's code from kaggle Titanic competition kernel:
grid = sns.FacetGrid(train_df, row='Embarked', size=2.2, aspect=1.6)
grid.map(sns.pointplot, 'Pclass', 'Survived', 'Sex', palette='deep')
grid.add_legend()
它会产生错误的情节,即颜色相反的情节.我想知道如何修复这个确切的代码片段.我尝试将关键字paramas添加到grid.map()调用-order=["male", "female"], hue_order=["male", "female"]
,但随后图变为空.
It produces wrong plot, the one with reversed colors. I'd like to know how to fix this exact code fragment. I tried adding keyword paramas to grid.map() call - order=["male", "female"], hue_order=["male", "female"]
, but then plots become empty.
推荐答案
在对grid.map(sns.pointplot, 'Pclass', 'Survived', 'Sex', palette='deep')
的代码调用中,x类别是Pclass
,而色相类别是Sex
.因此,您需要添加
In the code call to grid.map(sns.pointplot, 'Pclass', 'Survived', 'Sex', palette='deep')
, the x category is the Pclass
and the hue category is the Sex
. Hence you need to add
order = [1,2,3], hue_order=["male", "female"]
完整的示例(在这里我拿到了seaborn附带的泰坦尼克号-什么文字游戏!):
Complete example (where I took the titanic that ships with seaborn - what wordplay!):
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset("titanic")
grid = sns.FacetGrid(df, row='embarked', size=2.2, aspect=1.6)
grid.map(sns.pointplot, 'pclass', 'survived', 'sex', palette='deep',
order=[1,2,3], hue_order=["female","male"])
grid.add_legend()
plt.show()
请注意,尽管绝对需要hue_order
,但您可以省去order
.虽然这会发出警告,但正确的顺序是这些值是数字的,因此可以自动排序,从而确保了正确的顺序.
Note that while hue_order
is definitely required, you may leave out the order
. While this will throw a warning, the correct order is garantied by the fact that those values are numerical and are hence automatically sorted.
这篇关于如何订购seaborn pointplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!