Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hue parameter in seaborn FacetGrid

I am having a problem with Facetgrid: when I use the hue parameter, the x-labels show up 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 Hue: Facetgrid with Hue

From this it seems that there are more females than males, but this is not true.

If I now remove the hue option, then I get a correct distribution: there are more males than females across all pclasses.

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

Facetgrid without Hue: Facetgrid without Hue

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

like image 515
PeerEZ Avatar asked Apr 26 '16 11:04

PeerEZ


People also ask

What does FacetGrid do in Seaborn?

FacetGrid object takes a dataframe as input and the names of the variables that will form the row, column, or hue dimensions of the grid. The variables should be categorical and the data at each level of the variable will be used for a facet along that axis.

What is the purpose of FacetGrid?

A FacetGrid is a multi-axes grid with subplots visualizing the distribution of variables of a dataset and the relationship between multiple variables.

What is Col_wrap?

col_wrapint. “Wrap” the column variable at this width, so that the column facets span multiple rows. Incompatible with a row facet. share{x,y}bool, 'col', or 'row' optional. If true, the facets will share y axes across columns and/or x axes across rows.

How do you show multiple graphs in Seaborn?

In Seaborn, we will plot multiple graphs in a single window in two ways. First with the help of Facetgrid() function and other by implicit with the help of matplotlib. data: Tidy dataframe where each column is a variable and each row is an observation.


1 Answers

If you are going to use FacetGrid with one of the categorical plotting functions, you need to supply order information, either by declaring the variables as categorical or with the order and hue_order parameters:

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, it is generally preferable to use factorplot, which will take care of this bookkeeping for you and also save you some typing:

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

enter image description here

like image 172
mwaskom Avatar answered Oct 08 '22 16:10

mwaskom