【问题标题】:typeerror in basic example for legend handles in matplotlibmatplotlib 中图例句柄的基本示例中的 typeerror
【发布时间】:2016-11-30 17:59:07
【问题描述】:

我很难理解图例的处理方式。更多,基本例子来自官方matplotlib legend guide

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(handles=[line_up, line_down])

TypeError: __init__() got multiple values for keyword argument 'handles' 失败。

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

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

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

heiland@note121:bauHS15_iomapsgenpod$ python testleg.py 
Traceback (most recent call last):
  File "testleg.py", line 4, in <module>
    plt.legend(handles=[line_up, line_down])
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 3381, in legend
    ret = gca().legend(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 4778, in legend
    self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
TypeError: __init__() got multiple values for keyword argument 'handles'

【问题讨论】:

  • 这很奇怪...你能把 full 的追踪发回来吗?
  • 当然,我已将其添加到问题正文中。
  • 我无法在 1.4.0 上重现此内容。这要么是一个已经修复的错误,要么是你的安装过程中发生了一些非常有趣的事情。

标签: python matplotlib


【解决方案1】:

只需删除 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])

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我不知道。增加您的声誉不应该花费太长时间。我看到您试图提供帮助,但是a)“答案”不符合答案政策,这就是我提出建议的原因,b)现在似乎没有办法删除我的评论它已被输入,并且 c) 另外两个人提出了相同的建议,但还不够好,无法发表评论。
    【解决方案3】:

    前段时间我遇到了同样的错误,但上面建议的修复方法对我不起作用。我也更新了我的 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()
    

    很好地呈现了这一点:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多