0

I saw this method from an older post but can't get the plot I want.

To start

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

df = pd.DataFrame({'x':np.random.rand(10),'y':np.random.rand(10)}, 
                  index=list(string.ascii_lowercase[:10]))

scatter plot

ax = df.plot('x','y', kind='scatter', s=50)

Then define a function to iterate the rows to annotate

def annotate_df(row):  
    ax.annotate(row.name, row.values,
                xytext=(10,-5), 
                textcoords='offset points',
                size=18, 
                color='darkslategrey')

Last apply to get annotation

ab= df.apply(annotate_df, axis=1)

Somehow I just get a series ab instead of the scatter plot I want. Where is wrong? Thank you!

0

2 Answers 2

3

Your code works, you just need plt.show() at the end.

Your full code:

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

df = pd.DataFrame({'x':np.random.rand(10),'y':np.random.rand(10)}, 
                  index=list(string.ascii_lowercase[:10]))

ax = df.plot('x','y', kind='scatter', s=50)

def annotate_df(row):  
    ax.annotate(row.name, row.values,
                xytext=(10,-5), 
                textcoords='offset points',
                size=18, 
                color='darkslategrey')

ab= df.apply(annotate_df, axis=1)

plt.show()
Sign up to request clarification or add additional context in comments.

2 Comments

By the way, Is there a way to solve the overlapping issue of too many annotations?
1

Looks like that this doesn't work any more, however the solution is easy: convert row.values from numpy.ndarray to list:
list(row.values)

Comments

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.