1

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 ]

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
user12625679
  • 676
  • 8
  • 23
  • Does this answer your question? [Seaborn - change bar colour according to hue name](https://stackoverflow.com/questions/46173419/seaborn-change-bar-colour-according-to-hue-name) – Gusti Adli May 31 '21 at 15:28
  • Can you add your input dataframe? – Nk03 May 31 '21 at 15:37

1 Answers1

2

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

Alex
  • 6,610
  • 3
  • 20
  • 38