Matplotlib动画浪费了大量的内存

时间:2014-10-21 21:52:43

标签: python animation memory matplotlib pyside

我正在以PySide形式使用Matplolib进行动画制作。动画正在运行,一切正常。但它浪费了很多记忆。我提供了一些测试数据来测试下面的代码,也许它到了最后,但对于更大的数据,程序停止工作。我能做些什么来不浪费大量的记忆?

import sys
from matplotlib import pyplot, animation
import numpy as np
from PySide import QtCore, QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from moves_data import moves_x, moves_y

class form(QtGui.QWidget):

    Moves_x = moves_x
    Moves_y = moves_y

    def __init__(self):
        super(form, self).__init__()
        self.InitializeComponent()
        self.set_animation_area()

    def InitializeComponent(self):
        self.hbox = QtGui.QHBoxLayout()

        #----------------- ANIMATION BUTTON -----------------
        self.btn_anim = QtGui.QPushButton("Run Animation")
        self.btn_anim.clicked.connect(self.start_animation)
        self.hbox.addWidget(self.btn_anim)
        #----------------------------------------------------

        self.setLayout(self.hbox)
        self.show()

    def set_animation_area(self): #some layout config
        self.fig_anim = pyplot.figure()
        ax = pyplot.axes(xlim=(-0.5, 500 - 0.5), ylim=(-0.5, 500 - 0.5))
        self.lines = ax.plot([], [], '-g', lw=1)
        self.line = self.lines[0]
        ax.set_xticks(np.arange(-0.5, 500 - 0.5, 10))
        ax.set_yticks(np.arange(-0.5, 500 - 0.5, 10))
        ax.set_xticklabels([])
        ax.set_yticklabels([])
        ax.grid(True, color = 'gray', linestyle = '-')
        self.canvas = FigureCanvas(self.fig_anim)
        self.canvas.setParent(self)
        self.hbox.addWidget(self.canvas)

    def init_anim(self):
        self.line.set_data([], [])
        return self.line,

    #-------problem might be here!!-------
    def animate(self, i):
        run_x = []
        run_y = []
        k = 0
        while k <= self.aux:
            run_x.append(self.Moves_x[k])
            run_y.append(self.Moves_y[k])
            k += 1
        self.aux += 1
        self.line.set_data(run_x, run_y)
        return self.line,
    #-------------------------------------

    def start_animation(self):
        self.aux = 0
        self.canvas.close_event()
        self.anim = self.get_animation()

    def get_animation(self):
        return animation.FuncAnimation(self.fig_anim, self.animate, init_func = self.init_anim, frames = len(moves_x), interval = 10, blit=True, repeat=False)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    ex = form()
    sys.exit(app.exec_())

我认为我是重叠的线条,我试图通过遵循这个想法来修复它,但什么都没有。

Data Link

1 个答案:

答案 0 :(得分:1)

如果偶数Move_*是可切片的,我会这样做:

def animate(self, i):
    run_x = self.Moves_x[:i]
    run_y = self.Moves_y[:i]
    self.line.set_data(run_x, run_y)
    return self.line,

如果没有,那么

def animate(self, i):
    new_x = self.Moves_x[i]
    new_y = self.Moves_y[i]
    run_x = np.r_[self.line.get_xdata(), new_x]
    run_y = np.r_[self.line.get_ydata(), new_y]
    self.line.set_data(run_x, run_y)
    return self.line