2014-08-28 30 views
4

我很難理解圖例處理。從官方matplotlib legend guidematplotlib中的圖例句柄的基本示例中的typeerror

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腳本上面的線)

[email protected]: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' 
+0

也就是說奇...你能張貼_full_追溯? – tacaswell 2014-08-29 01:09:54

+0

當然,我已經將它添加到問題主體。 – Jan 2014-08-29 09:32:40

+1

我無法在1.4.0上重現這一點。這是一個已經修復的錯誤,或者是你的安裝有趣的事情。 – tacaswell 2014-08-29 12:37:57

回答

9

只是刪除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]) 
4

我和Jan有同樣的問題,在Ubuntu 14.04上運行Matplotlib 1.3.1。我試着Kobi K發佈的答案。他的代碼沒有提出任何錯誤。但是,圖例並沒有正確顯示: Bad legend via Matplotlib 1.3.1 我升級到Matplotlib 1.5.1,現在可以使用Jan發佈的代碼正確渲染圖例,其中包括'句柄'關鍵字(即代碼出現在Matplotlib legend guide ): Bad legend via matplotlib 1.5.1

+0

不是我所知道的。增加聲譽不應該花太長時間。我發現你試圖提供幫助,但是a)「答案」不符合答案政策,爲什麼我提出了建議,b)現在似乎沒有辦法刪除我的評論它已經進入了,c)另外兩個人提出了相同的建議,但不夠好留言。 – 2016-07-12 21:23:09

+0

我已刪除我的評論,並提出了您的答案。 – 2016-07-14 17:58:49

2

我曾經有過相同的錯誤,但上面提出的修復方法對我並不適用。我也更新了我的matplotlib版本,但這並沒有幫助。

什麼工作是去除句柄參數和哪個圖形標記在legend()方法中;像這樣:

plot1 = plt.plot([1,2,3], 'b', label = 'first plot') 
    plot2 = plt.plot([3,2,1], 'r', label = 'second plot') 
    plt.legend() 
    plt.show() 

這很好地呈現這樣的: enter image description here