Search code examples
pythonseabornpairplot

The hue parameter in searborn.pairplot() does not work


everybody. I'm trying to plot a pair plot of the IRIS dataset using searborn. To do this, I load the dataset as follows:

import pandas as pd
iris = pd.read_csv('iris.csv')

the dataset has four numerical features and one categorical class called variety (with 3 categories). I use the following simple code to pairplot my data:

import searborn as sns
sns.pairplot(iris, hue = iris['variety'])

But the code returns the following error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Is there anything that I missed? How can I fix this problem?


Solution

  • The parameter hue only corresponds to the name of the variable inside pandas.

    Hence, the correct code is:

    import seaborn as sns
    sns.pairplot(iris, hue = 'variety')
    

    As you can see, the only change is that we provide hue='variety' instead of the array itself (hue=iris['variety']).

    Source:

    Seaborn documentation, pairplot function.

    https://seaborn.pydata.org/generated/seaborn.pairplot.html