Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib动画会浪费大量内存_Python_Animation_Memory_Matplotlib_Pyside - Fatal编程技术网

Python Matplotlib动画会浪费大量内存

Python Matplotlib动画会浪费大量内存,python,animation,memory,matplotlib,pyside,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_qt

我正在用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_())
导入系统 从matplotlib导入pyplot,动画 将numpy作为np导入 从PySide导入QtCore、QtGui 从matplotlib.backends.backend_qt4agg导入FigureCanvas qtagg as FigureCanvas 从移动\数据导入移动\ x,移动\ y 类表单(QtGui.QWidget): 移动\u x=移动\u x Moves\u y=Moves\u y 定义初始化(自): super(form,self)。\uuuu init\uuuuu() self.InitializeComponent() self.set_动画_区域() def初始化组件(自身): self.hbox=QtGui.QHBoxLayout() #-----------------动画按钮----------------- self.btn_anim=QtGui.QPushButton(“运行动画”) self.btn\u动画。单击。连接(self.start\u动画) self.hbox.addWidget(self.btn_anim) #---------------------------------------------------- self.setLayout(self.hbox) self.show() def set_animation_area(self):#一些布局配置 self.fig_anim=pyplot.figure() ax=pyplot.axs(xlim=(-0.5500-0.5),ylim=(-0.5500-0.5)) self.lines=ax.plot([],[],'-g',lw=1) self.line=self.line[0] ax.set_xticks(np.arange(-0.5500-0.510)) 斧头套件(np.arange(-0.5500-0.510)) ax.setxticklabels([]) ax.设置标签([]) ax.grid(真,颜色='gray',线型='-') self.canvas=FigureCanvas(self.fig\u动画) self.canvas.setParent(self) self.hbox.addWidget(self.canvas) def初始动画(自身): self.line.set_数据([],[]) 返回自助线路, #-------问题可能就在这里------- def动画(self,i): 运行_x=[] run_y=[] k=0
虽然k如果连
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

这两个选项不断增加内存使用。第二个代码是:
run\ux=np.r\u[self.line.get\uxdata(),new\ux]run\uy=np.r\uy[self.line.get\uydata(),new\uy]
返回self.line,
?我看不懂你的第二条评论。在你的代码中是:
line.get\uxdata()
。需要把自我放在第一位吗?在
返回self.line
需要在行后加一个逗号吗?哦,是的,我去掉了self的,逗号不重要