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!

STORMHOLD
  • 33
  • 1
  • 5

2 Answers2

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()
YOBA
  • 2,759
  • 1
  • 14
  • 29
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)

user2848463
  • 123
  • 5