0
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'month':['jan','feb'], 'val':[1.3,2.4]})
df.plot(kind='bar', x='month', y='val')
plt.show()

As of now this displays a graph correctly, but how can I get the exact numerical value displayed somewhere above or on each bar? I've tried referencing this but it doesn't seem to work for dataframes.

1 Answers1

1

You just need to adapt your link:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'month':['jan','feb'], 'val':[1.3,2.4]})
df.plot(kind='bar', x='month', y='val')
for index, value in enumerate(list(df["val"])):
    plt.text(index, value, str(value))
plt.show()

Output:

enter image description here

Let's try
  • 970
  • 7
  • 19
  • ah, i did that exactly but kept trying to jam in `plt.barh(list(df['month']), list(df['val']))` before the loop. thanks! – stackoverflow Sep 15 '20 at 16:10
  • if you run the code on `df = pd.DataFrame({'month':['jan','feb','mar','apr','may'], 'val':[1,2,1,2,1]})` does it display correctly as well? For me the values are all scattered and don't show correctly. https://imgur.com/a/zCMI9MP – stackoverflow Sep 15 '20 at 16:15
  • 1
    I just copy-pasted your df into the code and its working – Let's try Sep 15 '20 at 16:29