数据帧Python中的标签点

数据帧Python中的标签点,python,pandas,matplotlib,Python,Pandas,Matplotlib,我想用x轴值标记熊猫中的数据点。我正在尝试将此解决方案应用到我的代码中: 我得到一个错误,说: AttributeError: 'PathCollection' object has no attribute 'text' 这是我的密码: def draw_scatter_plot(xaxis, yaxis, title, xaxis_label, yaxis_label, save_filename, color, figsize=(9, 7), dpi=100): fig = pl

我想用x轴值标记熊猫中的数据点。我正在尝试将此解决方案应用到我的代码中:

我得到一个错误,说:

AttributeError: 'PathCollection' object has no attribute 'text'
这是我的密码:

def draw_scatter_plot(xaxis, yaxis, title, xaxis_label, yaxis_label, save_filename, color, figsize=(9, 7), dpi=100):
    fig = plt.figure(figsize=figsize, dpi=dpi)

    ax = plt.scatter(xaxis, yaxis, c=color)

    plt.xlabel(xaxis_label)
    plt.ylabel(yaxis_label)

    label_point(xaxis, yaxis, xaxis, ax)

    plt.title(title)

    fig.savefig(save_filename, dpi=100)

# label code from https://stackoverflow.com/questions/15910019/annotate-data-points-while-plotting-from-pandas-dataframe/15911372#15911372

def label_point(x, y, val, ax):
    a = pd.concat({'x': x, 'y': y, 'val': val}, axis=1)
    for i, point in a.iterrows():
        ax.text(point['x'], point['y'], str(point['x']))

关于这个问题有什么建议吗?

请考虑以下演示:

In [6]: df = pd.DataFrame(np.random.randint(100, size=(10, 2)), columns=list('xy'))

In [7]: df
Out[7]:
    x   y
0  44  13
1  69  53
2  52  80
3  72  64
4  66  42
5  96  33
6  31  13
7  61  81
8  98  63
9  21  95

In [8]: ax = df.plot.scatter(x='x', y='y')

In [9]: df.apply(lambda r: ax.annotate(r['x'].astype(str)+'|'+r['y'].astype(str), 
                                       (r.x*1.02, r.y*1.02)), axis=1)
Out[9]:
0    Annotation(44,13,'44|13')
1    Annotation(69,53,'69|53')
2    Annotation(52,80,'52|80')
3    Annotation(72,64,'72|64')
4    Annotation(66,42,'66|42')
5    Annotation(96,33,'96|33')
6    Annotation(31,13,'31|13')
7    Annotation(61,81,'61|81')
8    Annotation(98,63,'98|63')
9    Annotation(21,95,'21|95')
dtype: object
结果:


考虑以下演示:

In [6]: df = pd.DataFrame(np.random.randint(100, size=(10, 2)), columns=list('xy'))

In [7]: df
Out[7]:
    x   y
0  44  13
1  69  53
2  52  80
3  72  64
4  66  42
5  96  33
6  31  13
7  61  81
8  98  63
9  21  95

In [8]: ax = df.plot.scatter(x='x', y='y')

In [9]: df.apply(lambda r: ax.annotate(r['x'].astype(str)+'|'+r['y'].astype(str), 
                                       (r.x*1.02, r.y*1.02)), axis=1)
Out[9]:
0    Annotation(44,13,'44|13')
1    Annotation(69,53,'69|53')
2    Annotation(52,80,'52|80')
3    Annotation(72,64,'72|64')
4    Annotation(66,42,'66|42')
5    Annotation(96,33,'96|33')
6    Annotation(31,13,'31|13')
7    Annotation(61,81,'61|81')
8    Annotation(98,63,'98|63')
9    Annotation(21,95,'21|95')
dtype: object
结果:


出现问题是因为您返回了
plt.scatter
名称
ax
。这是令人困惑的,因为它不是一个轴,而是一个
'PathCollection'
(错误告诉您)

更换前两行

fig = plt.figure(figsize=figsize, dpi=dpi)
ax = plt.scatter(xaxis, yaxis, c=color)


并保持代码的其余部分不变

出现问题是因为您返回了
plt.scatter
名称
ax
。这是令人困惑的,因为它不是一个轴,而是一个
'PathCollection'
(错误告诉您)

更换前两行

fig = plt.figure(figsize=figsize, dpi=dpi)
ax = plt.scatter(xaxis, yaxis, c=color)


并保持代码的其余部分不变

熊猫的绘图返回一个轴对象。您使用plt.scatter,它返回一个路径对象。@sascha哦,找到了。如何在此处应用注释功能?手动创建轴对象,使用它调用散射,然后将其传递给label func。我不确定什么是最佳实践,但是
f,ax=plt.subplot(1)
后面跟着
ax.scatter()
(可能
ax[0]
)可能会起作用。(虽然它看起来很傻,因为有子情节)@sascha我已经按照指导修复了代码。但是它什么也没显示。那么您可能应该显示您修改过的代码。编辑:或者使用MaxU更聪明的方法!熊猫的绘图返回一个轴对象。您使用plt.scatter,它返回一个路径对象。@sascha哦,找到了。如何在此处应用注释功能?手动创建轴对象,使用它调用散射,然后将其传递给label func。我不确定什么是最佳实践,但是
f,ax=plt.subplot(1)
后面跟着
ax.scatter()
(可能
ax[0]
)可能会起作用。(虽然它看起来很傻,因为有子情节)@sascha我已经按照指导修复了代码。但是它什么也没显示。那么您可能应该显示您修改过的代码。编辑:或者使用MaxU更聪明的方法!matplotlib和熊猫的完美结合!matplotlib和熊猫的完美结合!