0

How to print y-values above each bar, please? Thank you

import matplotlib.pyplot as plt
import seaborn as sns

data = [6, 3, 1, 5, 2, 7, 3, 1, 9, 2, 1, 4, 2, 6, 3, 9, 4, 1, 7, 3, 1, 9, 2, 1, 4, 2, 1, 3, 1, 4, 2, 7, 3, 1, 5, 2, 7, 3, 1, 5, 1, 8, 4, 1, 6, 3, 1, 5, 2, 7, 3, 1, 5, 2, 8, 4, 1, 6, 3, 9, 4, 1, 7, 2, 1, 3, 1, 4, 7, 3, 1, 9, 2, 1, 4, 2, 1, 3, 1, 4, 2, 7, 3, 1, 5, 2, 7, 3, 1, 5, 1, 8, 4, 1, 6, 3, 1, 5, 2, 7, 3, 1, 5, 2, 8, 4, 1, 6, 3, 9, 4, 1, 7, 2, 1, 3, 1, 4, 2, 7, 3, 1, 9, 2, 1, 4, 2, 6, 3, 9, 4, 2, 1, 5, 1, 8, 4, 1, 6, 3, 9, 4, 2, 1, 6, 1, 9, 4, 2, 7, 3, 1, 5, 1, 8, 4, 2, 1, 5, 1, 8, 4, 2, 1, 9, 4, 1, 7, 3, 1, 5, 1, 8, 2, 1, 6, 3, 9, 4, 2, 1, 5, 2, 1, 4, 2, 6, 3, 1, 5, 2, 1, 6, 3, 9, 4, 2, 7, 3, 1, 5, 2, 7, 3, 1, 5, 2, 8, 4, 1, 6, 3, 1, 5, 2, 7, 3, 1, 9, 2, 1, 4, 2, 6, 3, 9, 4, 1, 7, 3, 1, 9, 2, 1, 4, 2, 1, 3, 1, 4, 2, 7, 3, 1, 5, 2, 7, 3, 1, 5, 1, 8, 4, 1, 6, 3, 1, 5, 2]

fig, ax = plt.subplots()

ax = sns.histplot(data, discrete=True, kde=False, stat='percent') 

plt.show()
Carly
  • 113
  • 2
  • 9

1 Answers1

3

If you have matplotlib 3.4.0 or higher, you can use bar_label. More info here

Code would be something like this...

ax = sns.histplot(data, discrete=True, kde=False, stat='percent') 
ax.bar_label(ax.containers[0])
Redox
  • 9,321
  • 5
  • 9
  • 26
  • Thank you and how to add a format. I tried: `ax.bar_label('{:.2f} %'.format(ax.containers[0]))` – Carly May 11 '22 at 19:26
  • `ax.bar_label(ax.containers[0], fmt='%.2f%%')` – JohanC May 11 '22 at 19:34
  • Thanks @JohnC. Some info [here](https://stackoverflow.com/questions/68320207/how-to-get-the-label-values-on-a-bar-chat-with-seaborn-on-a-categorical-data/68322925#68322925) – Redox May 11 '22 at 19:36