2016-04-26 21 views
5

我有Facetgrid一個問題:當我使用色調參數,在X-標籤以錯誤的順序顯示,不匹配的數據。加載泰坦尼克號數據集的IPython:色調參數FacetGrid

%matplotlib inline 
import pandas as pd 
import matplotlib.pyplot as plt 
import seaborn as sns 

titanic = sns.load_dataset("titanic") 
g = sns.FacetGrid(titanic, col='pclass', hue='survived') 
g = g.map(sns.swarmplot, 'sex', 'age') 

Facetgrid與色調: Facetgrid with Hue

從這一點看來,有比男性更多的女性,但事實卻並非如此。

如果現在去掉色調選項,然後我得到一個正確的分佈:有比所有pclasses女性多男性。

g = sns.FacetGrid(titanic, col='pclass') 
g = g.map(sns.swarmplot, 'sex', 'age') 

Facetgrid無色相: Facetgrid without Hue

這是怎麼回事嗎? 我使用Seaborn 0.7.0

回答

3

如果你要使用FacetGrid用的分類繪圖功能之一,你需要,無論是通過聲明變量作爲分類或與orderhue_order參數提供訂單信息:

g = sns.FacetGrid(titanic, col='pclass', hue='survived') 
g = g.map(sns.swarmplot, 'sex', 'age', order=["male", "female"], hue_order=[0, 1]) 

enter image description here

但是,通常最好使用factorplot,這會照顧這個記賬的爲你,也爲您節省一些打字:

g = sns.factorplot("sex", "age", "survived", col="pclass", data=titanic, kind="swarm") 

enter image description here

+0

大,這使得它明確。謝謝 – PeerEZ