ArchieTiger
ArchieTiger

Reputation: 2243

Add text annotation to matplotlib plot from a pandas dataframe

How to add text annotations and mark plotted points and the numeric values to a matplotlib plot of a pandas dataframe? The columns of the dataframe are not a fixed size, they vary for different files.

dataframe = pd.read_csv('file1.csv')

plt.figure(figsize=(50,25))
dataframe.plot()
plt.xticks(rotation=45, fontsize=8)
plt.yticks(fontsize=8)

Upvotes: 7

Views: 7233

Answers (1)

MaThMaX
MaThMaX

Reputation: 2015

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df = pd.DataFrame({'x':range(0, 100, 20),
                   'y':np.random.randint(0,100,5)})

rows, cols = df.shape
fig, ax = plt.subplots(figsize=(50/10,25/10))
df.plot(ax=ax)

for col in range(cols):
    for i in range(rows):
        ax.annotate('{}'.format(df.iloc[i, col]), xy=(i, df.iloc[i, col])) 
plt.xticks(rotation=45, fontsize=8)
plt.yticks(fontsize=8)

plt.show()

enter image description here

Upvotes: 9

Related Questions