2015-12-29 22 views
0

代碼:兩種說法給出,但聲稱三個參數給出

import numpy as np 
import matplotlib.pyplot as plt 
l2_penalty = np.logspace(1, 7, num=13) 
plt.xscale('log',l2_penalty) 

我收到錯誤消息:

TypeError: set_xscale() takes exactly 2 arguments (3 given) 

爲什麼,我只給了兩個參數?

+4

您的堆棧跟蹤與您的代碼不匹配。 'plt.xscale' vs'set_xscale' –

+0

'xscale'調用什麼? – Bathsheba

+0

看起來你正在調用一些'xscale'函數,而另外一些正在調用,這可能是由於範圍問題,或者你可能正在顯示一些其他代碼並運行其他代碼。 – anand

回答

0

plt.xscale的呼叫簽名是xscale(*args, **kwargs),但在文檔中呼叫簽名實際上是plt.xscale(scale, **kwargs)

當函數被調用時,所有未命名的參數(通過*args傳入)將被傳遞到另一個函數內部。你們每個人都提供了分別送給其它功能的未命名的參數擴大了,所以(爲例):

def xscale(*args, *kwargs): 
    # Do some stuff 
    first_argument = 10 

    # Call another function taking only 2 arguments 
    other_function(first_argument, *args) 
    # This expands to: other_function(first_argument, 'log',l2_penalty) 
    # And since the target only takes 2 arguments and you provided 3 
    # It is this internal call that causes an error. 

    # Probably do some other stuff... 

它提供完整的堆棧跟蹤上SO很重要尋求幫助調試這些類型的時問題。我不得不運行你的代碼我自己,我希望它產生同樣的錯誤是你的,但我沒有被授權者...

Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
    File "/usr/lib64/python2.7/site-packages/matplotlib/pyplot.py", line 1502, in xscale 
    ax.set_xscale(*args, **kwargs) 
TypeError: set_xscale() takes exactly 2 arguments (3 given) 

如果你想在其他參數傳遞給plt.xscale功能你會需要在將它們作爲命名的參數,並按照文件:

'log' 

     *basex*/*basey*: 
      The base of the logarithm 

     *nonposx*/*nonposy*: ['mask' | 'clip' ] 
      non-positive values in *x* or *y* can be masked as 
      invalid, or clipped to a very small positive number 

     *subsx*/*subsy*: 
      Where to place the subticks between each major tick. 
      Should be a sequence of integers. For example, in a log10 
      scale: ``[2, 3, 4, 5, 6, 7, 8, 9]`` 

      will place 8 logarithmically spaced minor ticks between 
      each major tick. 

下面的示例使用命名參數可能是有用的:

plt.xscale('log', basex=10, basey=5) 
plt.xscale('log', subsx=[2, 3, 4]) 
2

檢查plot.xscale文檔。這是一種綁定方法,因此一個位置參數是self。另一個是像'日誌'這樣的詞。所有其他參數都需要關鍵字。'

set_xscale是一個採用相同參數的axes方法。 plot方法可能委託給axes方法。

相關問題