内存使用matplotlib动画 [英] Memory usage for matplotlib animation

查看:235
本文介绍了内存使用matplotlib动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从该页面的 http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/ ,使我自己的动画,但它很快崩溃。纵观任务管理器,我可以看到,从运行程序内存积聚上升到30秒,这是我的低于IM pressive笔记本电脑非常显著内1GB。从方式code打电话动画(i)向每次设置y_data就行了,不会被替换旧数据,导致内存积聚?我想解决这个问题。我在matplotlib的内部运作知识是有限的,有些事情我已经试过是把动画(I)的close(),CLF()和GC.Collect的(),但他们没有工作。

 
Matplotlib动画示例作者:杰克Vanderplas
电子邮件:vanderplas@astro.washington.edu
网站:http://jakevdp.github.com
许可:BSD
请随意使用和修改本,但保留了上述信息。谢谢!
导入numpy的是NP
从matplotlib进口pyplot如PLT
从matplotlib进口动画#先设置好身材,轴,我们要以动画的情节元素
图= plt.figure()
斧= plt.axes(XLIM =(0,2),ylim =( - 2,2))
行= ax.plot([],[],LW = 2)#初始化函数:积每帧的背景
高清的init():
    line.set_data([],[])
    返回线,#动画功能。这就是所谓的依次
高清动画(ⅰ):
    X = np.linspace(0,2,1000)
    Y = np.sin(2 * np.pi *(X - 0.01 * I))
    line.set_data(X,Y)
    返回线,#拨打动画。位块传输= TRUE表示只重新绘制已更改的部分。
动画= animation.FuncAnimation(图,动画,=的init_func初始化,
                               帧= 200,间隔= 20,blit的= TRUE)#保存动画为MP4。这需要的ffmpeg或男性codeR是
#安装。的extra_args确保的x264 codeC被使用,以便
#视频可以嵌入到HTML5。您可能需要调整本作
#您的系统:了解更多信息,请参阅
#http://matplotlib.sourceforge.net/api/animation_api.html
#anim.save('basic_animation.mp4',FPS = 30,extra_args = [' - V codeC','libx264'])plt.show()


解决方案

默认情况下,动画code试图保存帧的一定数量(默认为100)。尽量明确此设置为零:

 动画= animation.FuncAnimation(图,动画,=的init_func初始化,
                               帧= 200,间隔= 20,位图传送= TRUE,
                               save_count = 0)

I've been tweaking the code from this page http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/ to make my own animation, but it crashes very quickly. Looking at the task manager, I can see that the memory build-up from running the program rises to 1gb within 30 seconds, which is very significant for my less-than-impressive laptop. From the way the code was calling animation(i) to set the y_data on the line every time, is the old data not replaced, causing the memory build-up? I'd like to fix this. My knowledge on the inner workings of matplotlib is limited, and some things I've tried is putting close(), clf(), and gc.collect() in animation(i), but none of them worked.

"""
Matplotlib Animation Example

author: Jake Vanderplas
email: vanderplas@astro.washington.edu
website: http://jakevdp.github.com
license: BSD
Please feel free to use and modify this, but keep the above information. Thanks!
"""

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)

# initialization function: plot the background of each frame
def init():
    line.set_data([], [])
    return line,

# animation function.  This is called sequentially
def animate(i):
    x = np.linspace(0, 2, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
    return line,

# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=200, interval=20, blit=True)

# save the animation as an mp4.  This requires ffmpeg or mencoder to be
# installed.  The extra_args ensure that the x264 codec is used, so that
# the video can be embedded in html5.  You may need to adjust this for
# your system: for more information, see
# http://matplotlib.sourceforge.net/api/animation_api.html
# anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

plt.show()

解决方案

By default the animation code tries to save some number of frames (defaults to 100). Try explicitly setting this to zero:

anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=200, interval=20, blit=True,
                               save_count=0)

这篇关于内存使用matplotlib动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆