Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to label bubble chart/scatter plot with column from pandas dataframe?

I am trying to label a scatter/bubble chart I create from matplotlib with entries from a column in a pandas data frame. I have seen plenty of examples and questions related (see e.g. here and here). Hence I tried to annotate the plot accordingly. Here is what I do:

import matplotlib.pyplot as plt
import pandas as pd 
#example data frame
x = [5, 10, 20, 30, 5, 10, 20, 30, 5, 10, 20, 30]
y = [100, 100, 200, 200, 300, 300, 400, 400, 500, 500, 600, 600]
s = [5, 10, 20, 30, 5, 10, 20, 30, 5, 10, 20, 30]
users =['mark', 'mark', 'mark', 'rachel', 'rachel', 'rachel', 'jeff', 'jeff', 'jeff', 'lauren', 'lauren', 'lauren']

df = pd.DataFrame(dict(x=x, y=y, users=users)

#my attempt to plot things
plt.scatter(x_axis, y_axis, s=area, alpha=0.5)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.annotate(df.users, xy=(x,y))
    plt.show()

I use a pandas datframe and I somehow get a KeyError- so I guess a dict() object is expected? Is there any other way to label the data using with entries from a pandas data frame?

like image 710
Rachel Avatar asked Jan 05 '17 09:01

Rachel


2 Answers

You can use DataFrame.plot.scatter and then select in loop by DataFrame.iat:

ax = df.plot.scatter(x='x', y='y', alpha=0.5)
for i, txt in enumerate(df.users):
    ax.annotate(txt, (df.x.iat[i],df.y.iat[i]))
plt.show()

graph

like image 71
jezrael Avatar answered Sep 18 '22 01:09

jezrael


Jezreal's answer is fine, but i will post this just to show what i meant with df.iterrows in the other thread.

I'm afraid you have to put the scatter (or plot) command in the loop as well if you want to have a dynamic size.

df = pd.DataFrame(dict(x=x, y=y, s=s, users=users))

fig, ax = plt.subplots(facecolor='w')

for key, row in df.iterrows():
    ax.scatter(row['x'], row['y'], s=row['s']*5, alpha=.5)
    ax.annotate(row['users'], xy=(row['x'], row['y']))

enter image description here

like image 23
Rutger Kassies Avatar answered Sep 18 '22 01:09

Rutger Kassies