Skip to content
Advertisement

How to change color for only one bar using pyplot?

Currently the color of the bar are the same. How can I change the color of the bar with the highest value?

df.groupby("Position")["Salary"].mean()

#Plot bar graph
ax = df.groupby("Position")["Salary"].mean().plot(kind="bar",width = 0.8, color='darkblue')

plt.xlabel("Position",fontsize=12)
plt.ylabel("Salary",fontsize=12)
plt.title("Wages for different job functions", fontweight='bold', fontsize=14)

plt.show()

Advertisement

Answer

Try:

s = df.groupby("Position")["Salary"].mean()

s.plot.bar(color=np.where(s==s.max(), 'r','b'))

Output:

enter image description here

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement