Hue option on the seashore FacetGrid

I have a problem with Facetgrid: when I use the tint option, the x-labels appear in the wrong order and do not match the data. Loading the Titanic dataset in ipython:

%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 with a tint: Facetgrid with hue

This shows that there are more women than men, but this is not so.

If now the hue parameter is removed, then I get the correct distribution: in all pclasses there are more men than women.

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

Facetgrid no shade: Facetgrid without hue

What's going on here? I am using Seaborn 0.7.0

+5
source share
1 answer

If you intend to use FacetGrid with one of the categorical building functions, you need to specify order information by declaring the variables as categorical or with the order and hue_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

However, as a rule, it is preferable to use factorplot , which will take care of this bookkeeping for you, and also save you when entering text:

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

enter image description here

+3
source

Source: https://habr.com/ru/post/1247934/


All Articles