我正在尝试通过以下代码段将图例添加到我的绘图中:
import matplotlib.pylab as plt
fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # left, bottom, width, height (range 0 to 1)
axes.set_xlabel('x (m)')
axes.set_ylabel('y (m)')
for i, representative in enumerate(representatives):
axes.plot([e[0] for e in representative], [e[1] for e in representative], color='b', label='Representatives')
axes.scatter([e[0] for e in intersections], [e[1] for e in intersections], color='r', label='Intersections')
axes.legend()
我结束了这个情节
显然,项目在图中重复。我该如何纠正该错误?
正如文档所说,尽管很容易错过:
如果label属性为空字符串或以“ _”开头,则这些艺术家将被忽略。
因此,如果我要在循环中绘制相似的线条,而我只想在图例中显示一个示例线条,我通常会做类似的事情
ax.plot(x, y, label="Representatives" if i == 0 else "")
i
我的循环索引在哪里。
看起来不像分别构建它们那样好看,但是我经常想使标签逻辑尽可能地接近线条图。
(请注意,matplotlib
开发人员本身倾向于使用"_nolegend_"
显式的。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句