2017-03-17 67 views
1

我試圖使用plt.ion()在python使更新數字:tight_layout拋出錯誤:ValueError異常:MAX()arg是空序列

import numpy as np 
import numpy.matlib 
import matplotlib.pyplot as plt 

plt.ion() 

plt.close('all') 

tmax =30 
W = np.random.rand(6,100)   

fig, ax = plt.subplots(2,3)  
plt.show() 

for t in range (1, tmax): 
    W = t * W   
    for ii in range(2): 
     for jj in range(3):  
      output = W[3*ii+jj,:].reshape((10,10),order = 'F') 
      ax[ii,jj].clear() 
      ax[ii,jj].imshow(output, interpolation='nearest')     
    plt.tight_layout() 
    plt.draw() 
    plt.pause(0.1) 


plt.waitforbuttonpress() 

運行,這將顯示在控制檯空白數字和拋出錯誤:

Traceback (most recent call last):

File "", line 1, in runfile('/Users/Alessi/Documents/Spyder/3240ass/comp.py', wdir='/Users/Alessi/Documents/Spyder/3240ass')

File "/Users/Alessi/anaconda/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile execfile(filename, namespace)

File "/Users/Alessi/anaconda/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "/Users/Alessi/Documents/Spyder/3240ass/comp.py", line 27, in plt.tight_layout()

File "/Users/Alessi/anaconda/lib/python3.5/site-packages/matplotlib/pyplot.py", line 1387, in tight_layout fig.tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)

File "/Users/Alessi/anaconda/lib/python3.5/site-packages/matplotlib/figure.py", line 1752, in tight_layout rect=rect)

File "/Users/Alessi/anaconda/lib/python3.5/site-packages/matplotlib/tight_layout.py", line 322, in get_tight_layout_figure max_nrows = max(nrows_list)

ValueError: max() arg is an empty sequence

ps.using蟒蛇的Spyder

回答

1

在控制檯不能使用交互式多(ion)。不幸的是,問題不是很清楚你想要什麼;但假設您想要顯示一個帶有動畫效果的圖形窗口。一個簡單的方法可以在IPython控制檯之外運行腳本。在Spyder中,你可以進入Run/Configure(或按F6)並選擇「在新的專用Python控制檯中執行」。

enter image description here

現在用腳本本身的問題是你第一次調用plt.show(),這表明空的次要情節。這必須被刪除。

這將顯示一個動畫版將

import numpy as np 
import matplotlib.pyplot as plt 

plt.close('all') 
plt.ion() 

tmax =30 

fig, ax = plt.subplots(2,3)  

for t in range (1, tmax): 
    W = np.random.rand(6,100)  
    for ii in range(2): 
     for jj in range(3):  
      output = W[3*ii+jj,:].reshape((10,10),order = 'F') 
      ax[ii,jj].clear() 
      ax[ii,jj].imshow(output, interpolation='nearest') 
    if t == 1:     
     plt.tight_layout() 
    plt.draw() 
    plt.pause(0.1) 


plt.waitforbuttonpress() 
+0

對我來說最重要的部分是:「現在的劇本本身的問題是你第一次調用plt.show(),這表明空的次要情節。這必須被刪除。「 – Hakaishin