这是一个非常简单的例子:
xvalues = [2,3,4,6]
for x in xvalues:
plt.axvline(x,color='b',label='xvalues')
plt.legend()
Run Code Online (Sandbox Code Playgroud)
现在,图例将在图例中将'xvalues'显示为蓝线4次.有没有比以下更优雅的方法来解决这个问题?
for i,x in enumerate(xvalues):
if not i:
plt.axvline(x,color='b',label='xvalues')
else:
plt.axvline(x,color='b')
Run Code Online (Sandbox Code Playgroud) 我想用这个片段将图例添加到我的情节中:
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()
Run Code Online (Sandbox Code Playgroud)
我最终得到了这个情节

显然,这些项目在图中是重复的.我该如何更正此错误?