在matplotlib中的许多图上添加一个统一的传奇

发布于 2025-02-01 15:59:00 字数 582 浏览 2 评论 0原文

我正在绘制同一人物的几行(这是一个足球场),就像这样:

fig, ax = create_pitch(120, 80,'white')
for index, pass_ in cluster5.iterrows():
    if (index < 0):
        continue
    x, y = pass_['x'], pass_['y']
    end_x, end_y = pass_['end_x'], pass_['end_y']
    y = 80 - y
    end_y = 80 - end_y
    color = color_map[pass_['possession']]
    ax.plot([x, end_x], [y, end_y], linewidth = 3, alpha = 0.75, color = color, label = pass_['possession'])
ax.legend(loc = 'upper left')

有几个小组,我想为他们绘制一个传奇。 但是,我现在有一个重复的项目的传说(每个标签的每次呼叫ax图)。

我如何只为每个标签绘制一个传奇项目?

预先感谢!

I am plotting several lines in the same figure (which is a football pitch) like so:

fig, ax = create_pitch(120, 80,'white')
for index, pass_ in cluster5.iterrows():
    if (index < 0):
        continue
    x, y = pass_['x'], pass_['y']
    end_x, end_y = pass_['end_x'], pass_['end_y']
    y = 80 - y
    end_y = 80 - end_y
    color = color_map[pass_['possession']]
    ax.plot([x, end_x], [y, end_y], linewidth = 3, alpha = 0.75, color = color, label = pass_['possession'])
ax.legend(loc = 'upper left')

There are several groups and I would like to plot a single legend for them.
However, I now have a legend of repeated items (one for each call to ax plot for each label).

How can I just plot a single legend item for each label?

Thanks a lot in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦归所梦 2025-02-08 15:59:00

我通过添加代理图和句柄来解决此问题:

for c in color_labels:
    ax.plot(x, y, linewidth = 3, color = color_map[c], alpha = 0.75, label = c)

使用x,y是最后一个使用的曲线。使得最终颜色相同。

I solved this by adding a proxy plot and handle:

for c in color_labels:
    ax.plot(x, y, linewidth = 3, color = color_map[c], alpha = 0.75, label = c)

with x, y being the last used one. such that the final color is the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文