Search code examples
pandasconditional-statementsseabornhue

Seaborn hue with loc condition


I'm facing the following problem: I'd like to create a lmplot with seaborn and I'd like to distinguish the colors not based on an existing column but based on a condition adressed to a column.

Given the following df for a rental price prediction:

area rental price year build ...
40 400 1990 ...
60 840 1995 ...
480 16 1997 ...
... ... ... ...
sns.lmplot(x="area", y="rental price", data=df, hue = df.loc[df['year build'] > 1992])

this one above is not working. I know I can add a column representing this condition and adressing this column in "hue" but is there no way giving seaborn a condition to hue?

Thanks in advance!


Solution

  • You could add a new column with the boolean information and use that for the hue. For example data['at least from eighties'] = data['model_year'] >= 80. This will create a legend with the column name as title, and False and True as texts. If you map the values to strings, these will appear. Here is an example using one of seaborn's demo datasets:

    import matplotlib.pyplot as plt
    import seaborn as sns
    
    df = sns.load_dataset('mpg')
    df['decenium'] = (df['model_year'] >= 80).map({False: "seventies", True: "eighties"})
    
    sns.lmplot(x='weight', y='mpg', data=df, hue='decenium')
    plt.tight_layout()
    plt.show()
    

    sns.lmplot with hue over a condition