Skip to content Skip to sidebar Skip to footer

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.

Solution 1:

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 inenumerate(df.users):
    ax.annotate(txt, (df.x.iat[i],df.y.iat[i]))
plt.show()

graph

Solution 2:

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, rowin 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

Post a Comment for "How To Label Bubble Chart/scatter Plot With Column From Pandas Dataframe?"