Pee*_*rEZ 6 matplotlib seaborn
我遇到了Facetgrid的问题:当我使用hue参数时,x-labels以错误的顺序显示并且与数据不匹配.在ipython中加载Titanic数据集:
%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')
Run Code Online (Sandbox Code Playgroud)
Facetgrid与顺化:
从这一点看,似乎女性多于男性,但事实并非如此.
如果我现在删除了hue选项,那么我得到了正确的分布:所有pclasses中的男性多于女性.
g = sns.FacetGrid(titanic, col='pclass')
g = g.map(sns.swarmplot, 'sex', 'age')
Run Code Online (Sandbox Code Playgroud)
不带色调的Facetgrid:
这里发生了什么?我正在使用Seaborn 0.7.0
如果要FacetGrid
与分类绘图功能之一一起使用,则需要提供订单信息,方法是将变量声明为分类变量或使用order
和hue_order
参数:
g = sns.FacetGrid(titanic, col='pclass', hue='survived')
g = g.map(sns.swarmplot, 'sex', 'age', order=["male", "female"], hue_order=[0, 1])
Run Code Online (Sandbox Code Playgroud)
但是,通常最好使用factorplot
,它将为您处理此簿记工作,并且还节省了一些键入操作:
g = sns.factorplot("sex", "age", "survived", col="pclass", data=titanic, kind="swarm")
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9792 次 |
最近记录: |