pythoncolorsseaborndisplothistplot

color parameter isn't changing the plot color


Following is the code that doesn't work.

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

sns.displot(pd.DataFrame(res), color='red', edgecolor=None, binwidth=.5, binrange=(3, 18+1));

res is a list type.

I was expecting the bars to be red, but they are the default color.

I had no problem before with this code.


Solution

  • Imports and Data

    import numpy as np
    import seaborn as sns
    
    # random normal data as a list
    np.random.seed(2023)
    res = np.random.normal(loc=75, scale=5, size=1000).tolist()
    
    # create a dataframe
    df = pd.DataFrame(res)
    
    # df.head() - the column header is the number 0, not string '0'
    
               0
    0  78.558368
    1  73.377575
    2  69.990647
    3  76.181254
    4  74.489201
    

    Plot Implementation

    g = sns.displot(data=df, palette={0: 'r'})  # from v0.13.0 this does not work, because hue must also be specified
    g = sns.displot(data=df, palette=['r'])  # from v0.13.0 this does not work, because hue must also be specified
    
    g = sns.displot(data=df, x=0, color='r')
    
    g = sns.displot(x=df[0], color='r')
    
    g = sns.displot(data=res, color='r')
    
    g = sns.displot(x=res, color='r')
    

    enter image description here

    enter image description here