How to set a different color to the largest bar in a seaborn barplot?
[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)
The other answers defined the colors before plotting. You can as well do it afterwards by altering the bar itself, which is a patch of the axis you used to for the plot. To recreate iayork's example:
import seaborn
import numpy
values = numpy.array([2,5,3,6,4,7,1])
idx = numpy.array(list('abcdefg'))
ax = seaborn.barplot(x=idx, y=values) # or use ax=your_axis_object
for bar in ax.patches:
if bar.get_height() > 6:
bar.set_color('red')
else:
bar.set_color('grey')
You can as well directly address a bar via e.g. ax.patches[7]
. With dir(ax.patches[7])
you can display other attributes of the bar object you could exploit.
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")