我将这段代码拼凑在一起,从而打开了一个数据立方体,并沿着循环中的特定轴绘制了相关图像。该多维数据集是5D的,但另存为3d多维数据集,其中最后三个暗点以以下方式打包在一起。 idx = t*nw*ns + s*nw + w
,其中t为时间步长,s为斯托克斯参数,w为波长。打开它的包装并循环浏览很容易,但是当我运行这段代码一段时间(> 5s)时,它的速度会大大降低,如果我将其打开超过20s,它将冻结我的PC。
我认为这是因为我应该在过度绘制后清除图形,但是例程不希望在主循环中包含任何plt.clf()。有什么聪明的方法吗?
def animatecrisp(cube, dim, nw, nt, cut=50, t=0, s=0, w=0, ns=4):
fig = plt.figure()
'''
cubes are formated as an entire scan for one time and stokes.
Uses global vars: var, s, t, w, meanim, stdim
'''
#plot once to establish window
idx = t*nw*ns + s*nw + w
im = sl.get(cube,idx)[cut:-cut, cut:-cut]
global meanim, stdim #make global to use inside loop
meanim = np.mean(im)
stdim = np.std(im)
a = plt.imshow(im, vmax=meanim+2*stdim, vmin=meanim-2*stdim, cmap='gray', animated=True)
#Check which variable we are looping over
global var #make global to inside loop
var = 0
var_counter = 0
if dim == 't':
var_counter = nt
elif dim == 's':
var_counter = ns
elif dim == 'w':
var_counter = nw
else:
raise ValueError("Dim must be t,s or w.")
def updatefig(*args):
global var, s, t, w, meanim, stdim
var += 1
if var == var_counter:
var = 0
if dim == 't':
t = var
elif dim == 's':
s = var
elif dim == 'w':
w = var
else:
raise ValueError("Dim must be t,s or w.")
idx = t*nw*ns + s*nw + w
im = sl.get(cube,idx)[cut:-cut, cut:-cut]
meanim = np.mean(im)
stdim = np.std(im)
if mn==sd and mn==0:
a = plt.imshow(im, vmax=meanim+2*stdim, vmin=meanim-2*stdim, cmap='gray', animated=True)
else:
a = plt.imshow(im, vmax=mn+2*sd, vmin=mn-2*sd, cmap='gray', animated=True)
print(a,t,s,w, meanim, stdim)
return a,
ani = animation.FuncAnimation(fig, updatefig, fargs=(t,s,w), interval=50, blit=True)
plt.colorbar()
plt.show()