【发布时间】:2018-01-22 15:36:06
【问题描述】:
我想用 x 轴值标记 pandas 中的数据点。我正在尝试将此解决方案应用到我的代码中:Annotate data points while plotting from Pandas DataFrame
我收到一条错误消息:
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']))
对这个问题有什么建议吗?
【问题讨论】:
-
@sascha 哦,明白了。如何在这里应用注释功能?
-
手动创建一个轴对象,用它来调用分散,然后将它传递给你的标签函数。我不确定最佳做法是什么,但
f, ax = plt.subplots(1)后跟ax.scatter()(可能是ax[0]))可能会起作用。 (虽然它看起来很傻,因为 subplots) -
@sascha 我已按照指导修复了代码。但它什么也没显示。
-
那么您可能应该显示您修改后的代码。 编辑: 或使用 MaxU 更聪明的方法!
标签: python pandas matplotlib