相关疑难解决方法(0)

停止在图例中重复标签的matplotlib

这是一个非常简单的例子:

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)

matplotlib legend

55
推荐指数
4
解决办法
2万
查看次数

matplotlib中图例中的项目是否重复?

我想用这个片段将图例添加到我的情节中:

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)

我最终得到了这个情节

在此输入图像描述

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

python matplotlib

23
推荐指数
4
解决办法
2万
查看次数

标签 统计

matplotlib ×2

legend ×1

python ×1