【发布时间】:2019-06-20 18:32:35
【问题描述】:
Matplotlib 可以自动或手动显示图例,并为其提供图形句柄。但不知何故,后者对我来说不能正常工作。举个例子:
legend_handles = {}
lgh, = plt.plot([0, 1], [0, 1], '-r')
lgh, = plt.plot([0, 1], [1, 1], '-r')
legend_handles["a"] = lgh
lgh, = plt.plot([0, 1], [1, 0], '-b')
legend_handles["b"] = lgh
plt.legend(legend_handles);
这将给出一个带有两条红线的图例,而不是一条蓝线和一条红线。
如何让它只显示图例的选择?
【问题讨论】:
-
如果你用
legend_handles['a'], = plt.plot([0, 1], [1, 1], '-r')代替lgh, = plt.plot([0, 1], [1, 1], '-r'),b也一样,只是忽略第一个返回值,会发生什么?我不希望有什么不同,但我不在桌面上,所以我无法检查。 -
不应该是
lgh, = plt.plot([0, 1], [1, 0], '-b')吗?否则你的蓝线与红线重叠
标签: python matplotlib