STORMHOLD
STORMHOLD

Reputation: 33

Annotate labels in pandas scatter plot

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!

Upvotes: 0

Views: 5552

Answers (2)

user2848463
user2848463

Reputation: 123

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)

Upvotes: 1

YOBA
YOBA

Reputation: 2807

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()

Upvotes: 3

Related Questions