how to order seaborn pointplot

时光毁灭记忆、已成空白 提交于 2019-12-28 18:50:23

问题


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()

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.


回答1:


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"]

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()

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.



来源:https://stackoverflow.com/questions/46917425/how-to-order-seaborn-pointplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!