Home > Back-end >  Plotting a different color for each bin in a histogram (Matplotlib)
Plotting a different color for each bin in a histogram (Matplotlib)

Time:02-23

I have a pandas dataframe like this:

Favorite B | Q1
________________
McDonalds  | 5
BurgerKing | 6
KFC        | 3
Brand4     | 2

i am plotting histograms out of it:

x=pd.Series(df["Q1"])
result = plt.hist(x, bins=7, color='c', edgecolor='k', alpha=0.65)
plt.axvline(x.mean(), color='k', linestyle='dashed', linewidth=1)

min_ylim, max_ylim = plt.ylim()
plt.text(x.mean()*1.1, max_ylim*0.9, 'Mean: {:.2f}'.format(x.mean()))
plt.title(str(i))

I want a different color for each bin. How can I do it?

CodePudding user response:

This should work (based on histogram with differently colored bars

  • Related