7

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)

1 Answer 1

9
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

Sign up to request clarification or add additional context in comments.

1 Comment

I'm unable to explictly specify x and y. the columns are different in each input file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.