Displaying bar values in Pandas Plots

Hi everyone,
I got a quick question. for the below plot

plt.figure()

plt.rcParams.update(
    {'font.size': 20, 
     'axes.labelcolor': 'green',  
     'axes.titlecolor': 'orange'}
) 

ax1 = pd.DataFrame(train['meter'].value_counts(normalize = True)).plot(
    kind = 'bar',
    rot = 0,
    colormap = 'cividis',
    legend = False,
    figsize = (12, 8),
    xlabel = 'Meter Count',
    title = 'The frequency of each meter in the dataset'
)
ax1.set_ylabel('Percentage of Records', fontdict = {'fontsize' : 20})

Now If I would like to add the value of each bar above or inside it, how can it be done?
I’ve tried setting table = True but the table overlapped the x axis.

Thanks

1 Like

try using the code

for bar in ax1:
    height = bar.get_height()
    ax.text(bar.get_x() + bar.get_width() / 2,  height,  text, 
                     ha='center',  va='bottom')

This should help you add texts to the top of the bars on your bar plot. I hope this solves your problem.

2 Likes

Thanks for your help

your solution worked but with a small modification

labels = [f"{100*i:.2f}%" for i in df[‘col_name’]]
rects = ax1.patches
for rect, label in zip(rects, labels):
height = rect.get_height()
ax1.text(rect.get_x() + rect.get_width() / 2, height, label, ha=“center”, va=“bottom”)