【发布时间】:2018-08-08 23:07:53
【问题描述】:
我想创建至少 200*300 分辨率和 500 帧的 Matplotlib 热图动画。问题是我拍摄的动画的标准方法存在巨大的内存泄漏(如this question* 中所述)。当动画开始绘制或写出时,RAM 开始被填满,直到系统冻结,直到脚本被终止。太糟糕了,即使我的 4gb RAM 和 4gb 交换在一起也不够。除了创建更小的块并在那里一起编辑之外,还有什么方法可以制作该动画?
这是我的代码,稍加简化。 (注意:运行需要几分钟,可能会完全填满你的内存,导致死机。)
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.animation as animation
def waveanim(frames, trange, xbounds, ybounds, xnum, ynum, fps):
xpoints = np.linspace(*xbounds, xnum)
ypoints = np.linspace(*ybounds, ynum)
tmin, tmax = trange
# this part is a complicated calculation in my actual code; point is I have all the
# values for all the time points in an array that I calculate like this ready before
# I even start animating. (Since the calculation involves an inverse FFT, I can't just use NumPy's cool array managing abilities as ImportanceOfBeingErnest's answer suggests.)
result = np.empty((xnum, ynum, frames), dtype="float64")
for i, x in enumerate(xpoints):
print("calculating: {} out of {}".format(i, len(xpoints)), end='\r')
for j, y in enumerate(ypoints):
arr = np.array([np.sin(x+t) + np.cos(x-y-2*t) for t in np.linspace(tmin, tmax, frames)])
result[i,j] = arr
print('\n')
def animate(i):
print("animating: {} out of {}".format(i, frames), end='\r')
val = result[:,:,i].transpose()
pc = plt.pcolor(xpoints, ypoints, val, cmap='jet')
return pc,
fig, ax = plt.subplots()
im_ani = animation.FuncAnimation(fig, animate, frames=frames, interval=1000/fps, repeat_delay=0, blit=True)
plt.show()
def main():
trange = (-10.0, 10.0)
xbounds = (-20.0, 20.0)
ybounds = (-20.0, 20.0)
frames = 100
xnum = 300
ynum = 300
fps = 25
waveanim(frames, trange, xbounds, ybounds, xnum, ynum, fps)
if __name__ == '__main__':
main()
我还尝试单独生成图,将它们放入一个数组中,然后将该数组放入ArtistAnimation,就像在this example 中一样,但结果是一样的。
*我的不是骗子,因为 a) 我在 Kubuntu 上工作,而不是在 iOS 上工作,并且修复是特定于操作系统的,b) 因为我不限制解决泄漏错误的解决方案。
【问题讨论】:
标签: python-3.x animation matplotlib