run*_*chu 5 python matplotlib spyder
我试图在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()
Run Code Online (Sandbox Code Playgroud)
运行此命令将在控制台中显示空白数字并抛出错误:
Traceback(最近一次调用最后一次):
文件"",第1行,在runfile中('/ Users/Alessi/Documents/Spyder/3240ass/comp.py',wdir ='/ Users/Alessi/Documents/Spyder/3240ass')
文件"/Users/Alessi/anaconda/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py",第866行,在runfile execfile(filename,namespace)中
文件"/Users/Alessi/anaconda/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py",第102行,在execfile exec中(编译(f.read(),filename,'exec' ),命名空间)
文件"/Users/Alessi/Documents/Spyder/3240ass/comp.py",第27行,在plt.tight_layout()中
文件"/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 )
文件"/Users/Alessi/anaconda/lib/python3.5/site-packages/matplotlib/figure.py",第1752行,在tight_layout rect = rect)
文件"/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是一个空序列
ps.using anaconda spyder
ion您不能在控制台中使用交互式更多 ( )。不幸的是,这个问题不太清楚你想要什么;但假设您想要显示一个带有动画情节的窗口。实现这一点的一个简单方法是在 IPython 控制台之外运行脚本。在spyder中,您可以转到“运行/配置”(或按F6)并选择“在新的专用Python控制台中执行”。
现在脚本本身的问题是您首先调用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()
Run Code Online (Sandbox Code Playgroud)