2012-03-09 52 views
1

我試圖在使用plot()和matplottlib繪製圖時試圖解決一個問題。matplotlib plot()錯誤:TypeError:'NoneType'對象不可迭代

下面是代碼的片段,代碼例外,例外堆棧跟蹤和錯誤消息一起:

def line_graph(p_collection, filename, *args, **kwargs): 
    def plot(top): 
     temp = [p.foo_bar for p in p_collection] 
     print temp 
     top.plot(temp, '-', label="foobar") # <- This is line 801 
    multi_graph(p_collection, filename, plot, *args, **kwargs) 

輸出:

[276.25, 274.0, 257.0, 261.0, 259.75, 256.5, 261.0, 263.75, 257.5, 260.0, 251.0, 247.5, 246.0, 243.0, 243.75, 247.0, 244.0, 246.75, 251.0, 247.75, 245.75, 241.0, 251.0, 258.0, 258.75, 257.0, 262.0, 263.0, 256.0, 260.0, 261.0, 267.0, 264.25, 261.0, 268.5, 266.0, 268.25, 268.0, 271.0, 263.5, 259.0, 262.5, 265.5, 260.0, 261.5, 258.75, 258.25, 258.5, 255.0, 255.25, 251.75, 250.25, 256.5, 248.75, 249.5, 248.25, 246.0, 248.0, 248.5, 251.0, 254.5, 249.5, 248.5, 251.0, 253.0, 258.0, 259.0, 260.0, 262.0, 269.5, 259.25, 258.0, 250.5, 250.5, 250.5, 258.25, 255.5, 247.25, 248.0, 243.0, 247.5, 248.0, 251.25, 256.0, 250.0, 245.0, 241.5, 240.25, 247.5, 247.5, 246.0, 247.0, 250.0, 243.5, 235.5, 241.5, 249.5, 243.0, 243.5, 245.25] 
Traceback (most recent call last): 
    File "plotfuncs.py", line 1080, in <module> 
    unit_test() 
    File "plotfuncs.py", line 1072, in unit_test 
    left_view=(0, 40), 
    File "plotfuncs.py", line 802, in line_graph 
    multi_graph(phist, filename, plot, *args, **kwargs) 
    File "plotfuncs.py", line 594, in multi_graph 
    plot(top) 
    File "plotfuncs.py", line 801, in plot 
    top.plot(temp, '-', label="foobar") 
    File "/usr/local/lib/python2.6/dist-packages/matplotlib/axes.py", line 3853, in plot 
    self.autoscale_view(scalex=scalex, scaley=scaley) 
    File "/usr/local/lib/python2.6/dist-packages/matplotlib/axes.py", line 1842, in autoscale_view 
    x0, x1 = xlocator.view_limits(x0, x1) 
TypeError: 'NoneType' object is not iterable 

我不明白的內部Matplotlib的工作原理 - 但從傳遞給plot()函數的輸出數據可以看出,沒有None類型,所以我不明白爲什麼在迭代數據時在調用堆棧之後遇到一個NoneType結束 - 有人可以解釋嗎?

+1

我假設'top'是一個'Axis'。在801行801中調用劇情成員之前,已經做了些什麼? – 2012-03-09 14:22:10

回答

0

xlocator.view_limits(x0, x1)的輸出爲無,因此無法迭代將輸出展開爲x0, x1。我不知道這是爲什麼。我注意到這個錯誤發生在一個unit_test函數中,可能是matlibplot的單元測試。如果這是一個錯誤,它看起來像matlibplot的單元測試失敗,所以也許你應該報告它。

+0

Matlab - > matplotlib – 2012-03-09 14:18:18

+0

對不起 - 不知道我怎麼誤讀了!我會更新答案。 – aquavitae 2012-03-09 14:22:15