0
$\begingroup$

There is this dataframe:

print(df)

enter image description here

df.plot.bar(figsize=(10,5),fontsize=14,)
plt.title('counts',fontsize=15)

plt.show()

enter image description here barh:

df.plot(kind='barh')

How can I add value labels on this bar and barh chart?

$\endgroup$
1
$\begingroup$

Unfortunately, adding text to the plot must be done manually. See the example below.

fig, ax = plt.subplots(figsize=(10,5))
df.plot.bar(ax=ax, fontsize=14)
for i, v in enumerate(df['counts']):
    ax.text(i + .25, v + 3, str(v))

Similarly, you can do for the horizontal barplot.

fig, ax = plt.subplots(figsize=(10,5))
df.plot.barh(ax=ax, fontsize=14)
for i, v in enumerate(df['counts']):
    ax.text(v + 3, i + .25,  str(v))
$\endgroup$
1
  • $\begingroup$ thank you very much $\endgroup$
    – martin
    Oct 31 '20 at 1:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.