在遍历 pandas 数据框时如何在matplotlib散点图中标记数据点? [英] How to label data points in matplotlib scatter plot while looping through pandas dataframes?

查看:259
本文介绍了在遍历 pandas 数据框时如何在matplotlib散点图中标记数据点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫数据框,其中包括以下几列:

I have a pandas dataframe including the following columns:

label = ('A' , 'D' , 'K', 'L', 'P')
x = (1 , 4 , 9, 6, 4)
y = (2 , 6 , 5, 8, 9)
plot_id = (1 , 1 , 2, 2, 3)

我想创建3个单独的散点图-每个单独的plot_id都需要一个散点图.因此,第一个散点图应包含plot_id == 1的所有条目,并因此包含点(1,2)和(4,6).每个数据点都应用label标记.因此,第一个图应具有标签AB.

I want to creat 3 seperate scatter plots - one for each individual plot_id. So the first scatter plot should consists all entries where plot_id == 1 and hence the points (1,2) and (4,6). Each data point should be labelled by label. Hence the first plot should have the labels Aand B.

我知道我可以使用annotate进行标记,并且我熟悉for循环.但是我不知道如何将两者结合起来.

I understand I can use annotate to label, and I am familiar with for loops. But I have no idea how to combine the two.

我希望我可以发布我到目前为止所做的更好的代码片段-但这太糟糕了.在这里:

I wish I could post better code snippet of what I have done so far - but it's just terrible. Here it is:

for i in range(len(df.plot_id)):
    plt.scatter(df.x[i],df.y[i])
    plt.show()

仅此而已-不幸的是.关于如何进行的任何想法?

That's all I got - unfortunately. Any ideas on how to procede?

推荐答案

更新后的答案
保存单独的图像文件

updated answer
save separate image files

def annotate(row, ax):
    ax.annotate(row.label, (row.x, row.y),
                xytext=(10, -5), textcoords='offset points')

for pid, grp in df.groupby('plot_id'):
    ax = grp.plot.scatter('x', 'y')
    grp.apply(annotate, ax=ax, axis=1)
    plt.savefig('{}.png'.format(pid))
    plt.close()

1.png

1.png

2.png

2.png

3.png

3.png

旧答案
对于那些想要这样的人

old answer
for those who want something like this

def annotate(row, ax):
    ax.annotate(row.label, (row.x, row.y),
                xytext=(10, -5), textcoords='offset points')

fig, axes = plt.subplots(df.plot_id.nunique(), 1)
for i, (pid, grp) in enumerate(df.groupby('plot_id')):
    ax = axes[i]
    grp.plot.scatter('x', 'y', ax=ax)
    grp.apply(annotate, ax=ax, axis=1)
fig.tight_layout()

设置

setup

label = ('A' , 'D' , 'K', 'L', 'P')
x = (1 , 4 , 9, 6, 4)
y = (2 , 6 , 5, 8, 9)
plot_id = (1 , 1 , 2, 2, 3)

df = pd.DataFrame(dict(label=label, x=x, y=y, plot_id=plot_id))

这篇关于在遍历 pandas 数据框时如何在matplotlib散点图中标记数据点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆