python - How to add data labels to seaborn barplot? - Stack Overflow

PHOTO EMBED

Sat Dec 04 2021 03:20:57 GMT+0000 (Coordinated Universal Time)

Saved by @Mukil #python

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
print(df):
    A   B   C   D
0   31  11  65  15
1   83  21   5  87
2   16   6  81  41
3   91  78  95  70
4   26  51  26  61
..  ..  ..  ..  ..
95  31  18  91  24
96  73  97  42  45
97  76  22   2  36
98  12  43  98  27
99  33  96  67  68


probbins = [0,10,20,30,40,50,60,70,80,90,100]
df['Groups'] = pd.cut(df['D'],bins=probbins)
plt.figure(figsize=(15,6))
chart = sns.barplot(x=df['Groups'], y=df['C'],estimator=sum,ci=None)
chart.set_title('Profit/Loss')
chart.set_xticklabels(chart.get_xticklabels(), rotation=30)
# annotation here
for p in chart.patches:
             chart.annotate("%.0f" % p.get_height(), (p.get_x() + p.get_width() / 2., p.get_height()),
                 ha='center', va='center', fontsize=10, color='black', xytext=(0, 5),
                 textcoords='offset points')
plt.show()
content_copyCOPY

https://stackoverflow.com/questions/62002434/how-to-add-data-labels-to-seaborn-barplot