Python matplotlib中图例句柄的基本示例中的typeerror

Python matplotlib中图例句柄的基本示例中的typeerror,python,matplotlib,Python,Matplotlib,我很难理解图例处理。更重要的是,来自官方的基本例子 失败,错误为TypeError:\uuuu init\uuuu()为关键字参数“handles”获取了多个值。 我做错了什么?有什么想法吗 我的matplotlib版本是1.3.1。我在Ubuntu 14.04上 以下是完整的回溯(在python脚本中包含上述行) heiland@note121:bauHS15_iomapsgenpod$python testleg.py 回溯(最近一次呼叫最后一次): 文件“testleg.py”,第4行,在

我很难理解图例处理。更重要的是,来自官方的基本例子

失败,错误为
TypeError:\uuuu init\uuuu()为关键字参数“handles”获取了多个值。

我做错了什么?有什么想法吗

我的matplotlib版本是
1.3.1
。我在Ubuntu 14.04上

以下是完整的回溯(在python脚本中包含上述行)

heiland@note121:bauHS15_iomapsgenpod$python testleg.py
回溯(最近一次呼叫最后一次):
文件“testleg.py”,第4行,在
plt.legend(句柄=[line\u up,line\u down])
图例中第3381行的文件“/usr/lib/pymodules/python2.7/matplotlib/pyplot.py”
ret=gca().图例(*args,**kwargs)
图例中第4778行的文件“/usr/lib/pymodules/python2.7/matplotlib/axes.py”
self.legend uz=mlegend.legend(self、句柄、标签、**kwargs)
TypeError:\uuuu init\uuuuuu()为关键字参数“handles”获取了多个值

只需删除
句柄
关键字

这样使用:

import matplotlib.pyplot as plt
line_up, = plt.plot([1,2,3], label='Line 2')
line_down, = plt.plot([3,2,1], label='Line 1')
plt.legend([line_up, line_down])

我的问题和Jan一样,在Ubuntu 14.04上运行Matplotlib 1.3.1。我尝试了Kobi K发布的答案。他的代码没有出现任何错误。但是,图例未正确呈现: 我升级到Matplotlib 1.5.1,现在可以使用Jan发布的代码正确呈现图例,其中包括“handles”关键字(即显示在中的代码):

不久前我也遇到了同样的错误,但上面建议的修复方法对我不起作用。我也更新了matplotlib的版本,但这没有帮助

所做的工作是在legend()方法中删除handles参数和要标记的绘图;像这样:

    plot1 = plt.plot([1,2,3], 'b', label = 'first plot')
    plot2 = plt.plot([3,2,1], 'r', label = 'second plot')
    plt.legend()
    plt.show()
这很好地体现了这一点:

这太奇怪了……你能把完整的追溯贴出来吗?当然,我已经把它添加到问题正文中了。我无法在1.4.0上重现这一点。这要么是一个已经修复的bug,要么是你的安装过程中发生了一些非常有趣的事情。据我所知不是这样。不应该花太长时间来提高你的声誉。我知道你是在试图提供帮助,但是a)“答案”不符合答案策略,这就是我提出建议的原因;b)既然已经输入了我的评论,似乎没有办法删除;c)另外两个人提出了相同的建议,但没有留下评论。
import matplotlib.pyplot as plt
line_up, = plt.plot([1,2,3], label='Line 2')
line_down, = plt.plot([3,2,1], label='Line 1')
plt.legend([line_up, line_down])
    plot1 = plt.plot([1,2,3], 'b', label = 'first plot')
    plot2 = plt.plot([3,2,1], 'r', label = 'second plot')
    plt.legend()
    plt.show()