简体   繁体   English

tight_layout throw error:ValueError:max()arg是一个空序列

[英]tight_layout throw error: ValueError: max() arg is an empty sequence

I am trying to using plt.ion() in python enable updating the figures: 我试图在python中使用plt.ion()来更新数字:

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

Running this will show blank figures in console and throw errors: 运行此命令将在控制台中显示空白数字并抛出错误:

Traceback (most recent call last): Traceback(最近一次调用最后一次):

File "", line 1, in runfile('/Users/Alessi/Documents/Spyder/3240ass/comp.py', wdir='/Users/Alessi/Documents/Spyder/3240ass') 文件“”,第1行,在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) 文件“/Users/Alessi/anaconda/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py”,第866行,在runfile execfile中(文件名,命名空间)

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) 文件“/Users/Alessi/anaconda/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py”,第102行,在execfile exec中(编译(f.read(),filename,'e​​xec' ),命名空间)

File "/Users/Alessi/Documents/Spyder/3240ass/comp.py", line 27, in plt.tight_layout() 文件“/Users/Alessi/Documents/Spyder/3240ass/comp.py”,第27行,在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) 文件“/Users/Alessi/anaconda/lib/python3.5/site-packages/matplotlib/pyplot.py”,第1387行,在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) 文件“/Users/Alessi/anaconda/lib/python3.5/site-packages/matplotlib/figure.py”,第1752行,在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) 文件“/Users/Alessi/anaconda/lib/python3.5/site-packages/matplotlib/tight_layout.py”,第322行,在get_tight_layout_figure中max_nrows = max(nrows_list)

ValueError: max() arg is an empty sequence ValueError:max()arg是一个空序列

ps.using anaconda spyder ps.using anaconda spyder

You cannot use interactive more ( ion ) in a console. 您无法在控制台中使用交互式更多( ion )。 Unfortunately the question is not very clear on what you want; 不幸的是,问题并不是很清楚你想要什么; but let's suppose you want to show a window with the plot that is animated. 但是我们假设您要显示一个带有动画图的窗口。 An easy way to get that would be to run the script outside an IPython console. 一个简单的方法是在IPython控制台之外运行脚本。 In spyder you'd go to Run/Configure (or press F6) and select "Execute in a new dedicated Python console". 在spyder中,您将转到运行/配置(或按F6)并选择“在新的专用Python控制台中执行”。

在此输入图像描述

Now the problem with the script itself is that you first call plt.show() , which shows empty subplots. 现在脚本本身的问题是你首先调用plt.show() ,它显示空的子图。 This has to be removed. 这必须删除。

A version which would show an animation would be 将显示动画的版本

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM