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

此生再无相见时 提交于 2019-11-30 07:23:12

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

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']))

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!