紧密布局抛出错误:ValueError:max()arg是空序列

2024-06-16 09:30:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用血小板()在python中启用更新图形:

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.使用水蟒spyder


Tags: inpymatplotliblibpackageslinesiteplt
1条回答
网友
1楼 · 发布于 2024-06-16 09:30:05

不能在控制台中使用interactive more(ion)。不幸的是,这个问题并不是很清楚您想要什么;但是让我们假设您想要显示一个窗口,其中包含动画的绘图。一个简单的方法是在IPython控制台外运行脚本。在spyder中,您可以进入Run/Configure(或按F6键),然后选择“Execute In a new dedicated Python console”。在

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()

相关问题 更多 >