Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn pairplot hue parameter not working as expected

If "C" was assigned as value for the "hue" parameter, it was expected Seaborn not displayed column "C". Am I wrong?

sns.pairplot(df, hue='C')

enter image description here

DataFrame:

enter image description here

like image 763
Daniel Silva Avatar asked Aug 30 '18 18:08

Daniel Silva


1 Answers

By default, seaborn will show all numeric columns!

So if your 'hue' column ('C' in your case) column as string(object) type, it will not be visible on the graph

For instance:

import numpy as np
import pandas as pd
import seaborn as sns

data = {
    'A': [*np.random.random(5)],
    'B': [*np.random.random(5)],
    'C': ['X', 'Y', 'X', 'X', 'Y']
}

df = pd.DataFrame(data)

enter image description here

sns.set(style="ticks", color_codes=True)
sns.pairplot(df, hue='C')

enter image description here

However, if you have 'C' column as the numerical values, you have to use 'vars' to specify what columns you are going to use:

vars : list of variable names, optional

Variables within data to use, otherwise use every column with a numeric datatype.

data = {
    'A': [*np.random.random(5)],
    'B': [*np.random.random(5)],
    'C': [*np.random.randint(1, 3, 5)]
}

df = pd.DataFrame(data)

enter image description here

sns.set(style="ticks", color_codes=True)
sns.pairplot(df, hue='C', vars=['A', 'B'])

enter image description here

like image 192
Vlad Bezden Avatar answered Sep 29 '22 14:09

Vlad Bezden