Search code examples
pythonpandasmatplotlibseaborn

How to change colors based on the two other rows in Seaborn barplot


I have a dataframe like this:

index = ['Col-45', 'Col-68', 'Col-17', 'Col-69', 'Col-43', 'Col-49', 'Col-91',
       'Col-13', 'Col-14', 'Col-18', 'Col-38', 'Col-37', 'Col-40', 'Col-44',
       'Col-32', 'Col-82', 'Col-75', 'Col-19', 'Col-5', 'Col-6', 'Col-16',
       'Col-4', 'Col-7', 'Col-41', 'Col-10', 'Col-31', 'Col-12', 'Col-11',
       'Col-42', 'Col-30', 'Col-76', 'Col-46', 'Col-83', 'Col-73', 'Col-63',
       'Col-9', 'Col-28', 'Col-51', 'Col-74', 'Col-65', 'Col-50', 'Col-64',
       'Col-86', 'Col-79', 'Col-80', 'Col-81', 'Col-55', 'Col-1', 'Col-57',
       'Col-2', 'Col-61', 'Col-53', 'Col-88', 'Col-47', 'Col-3', 'Col-58',
       'Col-29', 'Col-59', 'Col-8', 'Col-276', 'Col-56', 'Col-62', 'Col-52',
       'Col-54']
Brand = ['LG','LG','LG','LG','LG', 'LG', 'LG', 'LG', 'LG', 'LG', 'LG', 'LG', 'LG', 'LG', 'LG', 'LG', 'Vivo',
         'Vivo', 'Vivo', 'Vivo', 'Vivo', 'Vivo', 'Vivo', 'Sony', 'Sony', 'Sony', 'Sony', 'Sony', 'Sony', 'Sony', 
         'Pixel', 'Pixel', 'Pixel', 'Pixel', 'Huawei', 'Huawei', 'Huawei', 'Apple', 'Apple', 'Apple', 'Xiaomi',
         'Xiaomi', 'Xiaomi', 'Lenovo', 'Lenovo', 'Lenovo', 'Panasonic', 'Panasonic', 'Panasonic', 'Beetle', 
         'Beetle', 'Samsung', 'Samsung', 'Nothing', 'Nothing', 'Nikon', 'Nikon', 'Canon', 'Canon', 'Coby', 
         'Coby', 'Onida', 'Amara', 'Roxy']

Score = [4.75, 0.91, 0.79, 0.65, 0.62, 0.57, 0.38, 0.33, 0.27, 0.25, 0.25,
       0.22, 0.16, 0.11, 0.02, 0.01, 3.89, 3.08, 2.1 , 1.75, 0.42, 0.27,
       0.18, 4.44, 1.18, 0.8 , 0.74, 0.52, 0.25, 0.08, 1.13, 0.75, 0.54,
       0.04, 1.03, 0.11, 0.  , 5.53, 5.24, 4.98, 0.98, 0.78, 0.06, 0.76,
       0.28, 0.04, 1.1 , 0.38, 0.25, 0.98, 0.01, 1.17, 0.61, 0.29, 0.19,
       0.12, 0.01, 0.13, 0.  , 4.37, 3.59, 0.53, 0.39, 1.3 ]

Choice = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,
       0, 0]

Result = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0,
       0]

df = pd.DataFrame({'index':index,'Brand':Brand,'Score':Score,'Choice':Choice,'Result':Result}).set_index('index').T

I created a barplot for Score index in the dataframe using this code:

fig = plt.figure()
sns.set(rc={'figure.figsize': (15,4)})

g1 = sns.barplot(data=df.loc[['Score']],color='blue')
g1.patch.set_edgecolor('black')  
g1.patch.set_linewidth(0.5)  
g1.set_facecolor('white')
g1.set_ylabel(f'Brand',weight='bold',fontsize=15)
g1.set_xlabel(None)
g1.set_xticklabels(g1.get_xticklabels(),rotation=90,fontsize=10)
plt.show()

I want to change the colors of the bars based on the values in index rows Choice(df.loc['Choice']) & Result(df.loc['Result']).

If (Choice=0 & Result=0), then bar color = blue

Else If (Choice=1 & Result=0), then bar color = green.

Else If (Choice=0 & Result=1), then bar color = pink.

Else If (Choice=1 & Result=1), then bar color = red.

Can anyone help me with this?


Solution

  • You can pass a hue with a custom palette:

    g1 = sns.barplot(y=df.loc['Score'], x=df.columns, 
                     hue=df.loc['Choice'] + df.loc['Result']*2, 
                     dodge=False, palette=['blue','green','pink','red']
                    )
    

    Output:

    enter image description here