Set color for specific bar in catplot

2.5k Views Asked by At

For the code below I am trying to create a barplot. If the column name in column ACQUISITION_CHANNEL = 'Referral' then the bar should be red else grey.

g = sns.catplot(
    data=df, kind="bar",
    x="CITY", y="CUSTOMERS", hue="ACQUISITION_CHANNEL",
    ci="sd", palette=clrs, alpha=.6, height=8
)
g.despine(left=False)
g.set_axis_labels("", "Share Total Customers for each City (%)")
g.legend.set_title("")

This is what I have tried so far, but it did not work

values = df.Customers
clrs = ['grey' if (x < max(values)) else 'red' for x in values ]

1

There are 1 best solutions below

0
On BEST ANSWER

You can specify a dict that maps the values used for the hue parameter to matplotlib colours, see the second example under point plots.

df = sns.load_dataset("tips")
palette = {c: "grey" if c != "Male" else "r" for c in df["sex"].unique()}
# {'Female': 'grey', 'Male': 'r'}

g = sns.catplot(
    data=df, 
    kind="bar",
    x="day", 
    y="total_bill", 
    hue="sex",
    ci="sd", 
    palette=palette, 
    alpha=.6, 
)

enter image description here