如何向图例添加注释箭头

2024-09-30 14:35:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个机器人的轨迹,以及一些描述机器人参数的箭头。就像这样: 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给出)添加到我的图例中呢?你知道吗


Tags: plotlocalfig绘制机器人箭头labelxy
1条回答
网友
1楼 · 发布于 2024-09-30 14:35:47

正如@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

相关问题 更多 >