我有一个机器人的轨迹,以及一些描述机器人参数的箭头。就像这样:Robot Trajectory with annotation arrows (因为我没有名气,我不能直接添加图片)
问题是:如何将注释箭头与图例中的线条一起显示?
我使用注释箭头,以便在循环中根据此answer绘制箭头,以便为每个点绘制箭头。下面是我的一个注解的代码:
an = ax.annotate('', xy=xy_tuple, xytext=xy_texttuple, label=labelString, arrowprops=dict(color=arrowcolor, arrowstyle=aStyle))
作为参考,我像这样使用绘图函数:
# plot Local x-y axis
fig, ax1 = plt.subplots()
ln1 = ax1.plot(x, y, '-o', label='Location of the last 48h')
ax1.set_ylabel('Local North (m)')
ax1.set_xlabel('Local East (m)')
ax1.grid()
fig.gca().set_aspect('equal', adjustable='box')
lns = ln1
labs = [l.get_label() for l in lns]
ax1.legend(lns, labs, loc='best')
那么,如何将注释标签(由labelString
提供)添加到图例中呢?
发布于 2019-08-05 14:40:03
正如@ImportanceOfBeingErnest所指出的,这是一个在matplotlib中发现的开放问题。开发人员想要解决这个问题,但是根据matplotlib developerboard上的这个discussion,一个解决方案已经实现,但没有通过最终版本,现在已经关闭(尽管它是一个相当酷的解决方案,imho)。
给出一行和两个注释,我的解决方案现在看起来是这样的。当然,这并不是很好:
ax1.legend([ln1, an1.arrow_patch, an2.arrow_patch], (ln1.get_label(), an1.get_label(), an2.get_label()))
产生以下example
https://stackoverflow.com/questions/57305208
复制