How to display exact values in a matplotlib pyplot made from a pandas DataFrame?

Question:

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.

Asked By: stackoverflow

||

Answers:

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

Answered By: Let's try