How to set a different color to the largest bar in

2020-02-23 07:59发布

问题:

I'm trying to create a barplot where all bars smaller than the largest are some bland color and the largest bar is a more vibrant color. A good example is darkhorse analytic's pie chart gif where they break down a pie chart and end with a more clear barplot. Any help would be appreciated, thank you!

回答1:

Just pass a list of colors. Something like

values = np.array([2,5,3,6,4,7,1])   
idx = np.array(list('abcdefg')) 
clrs = ['grey' if (x < max(values)) else 'red' for x in values ]
sb.barplot(x=idx, y=values, palette=clrs) # color=clrs)

(As pointed out in comments, later versions of Seaborn use "palette" rather than "color")



回答2:

[Barplot case] If you get data from your dataframe you can do these:

labels = np.array(df.Name)
values = np.array(df.Score) 
clrs = ['grey' if (x < max(values)) else 'green' for x in values ]
#Configure the size
plt.figure(figsize=(10,5))
#barplot
sns.barplot(x=labels, y=values, palette=clrs) # color=clrs)
#Rotate x-labels 
plt.xticks(rotation=40)