【问题标题】:How to add annotation arrows to legend如何将注释箭头添加到图例
【发布时间】:2019-08-01 08:52:07
【问题描述】:

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

【问题讨论】:

标签: python matplotlib annotate arrows


【解决方案1】:

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

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

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

导致以下example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 2015-07-22
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    相关资源
    最近更新 更多