首页 > 解决方案 > 如何将注释箭头添加到图例

问题描述

我有一些机器人的轨迹,以及一些描述机器人参数的箭头。就像这样: Robot Trajectory with annotation arrows (由于我的声誉不足,无法直接添加图片)

问题是:如何将注释箭头与图例中的线条一起显示?

我使用注释箭头来根据这个答案在循环中绘制箭头,为每个点绘制箭头。这是我的注释之一的代码:

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)添加到我的图例中?

标签: pythonmatplotlibannotatearrows

解决方案


正如@ImportanceOfBeingErnest 所指出的,这是一个在matplotlib 中发现的未解决问题。开发人员想要解决这个问题,但是根据matplotlib 开发板上的讨论,一个解决方案已经实现,但没有传递到最终版本,现在已经关闭(尽管这是一个非常酷的解决方案恕我直言)。

给出一行和两个注释,我的解决方案现在看起来像这样。当然,这不是那么好:

ax1.legend([ln1, an1.arrow_patch, an2.arrow_patch], (ln1.get_label(), an1.get_label(), an2.get_label()))

导致以下示例


推荐阅读