Matplotlib动画。FuncAnimation在运行python脚本时增加了内存使用量

时间:2019-02-23 13:15:48

标签: python python-3.x matplotlib

我是python编程的新手,我正在尝试创建具有动态图(动画)的GUI。我正在使用animation.FuncAnimation(),并且在脚本运行时,内存使用量随时间增加。 animation.FuncAnimation()每500毫秒从文件读取一次数据。有没有办法解决此问题并停止内存使用增量?

import tkinter as tk
import numpy as np

import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import matplotlib.animation as animation


figure_1 = Figure()
figure_2 = Figure()
plot_1 = figure_1.add_subplot(111)
plot_2 = figure_2.add_subplot(111)


def animate(i):
    global plot_1
    x_plot_1_1 = np.array([])
    y_plot_1_1 = np.array([])
    pullData = open('text_1.txt', "r").read()
    dataList = pullData.split('\n')

    if dataList == ['']:
        print('empty')
    else:
        for eachLine in dataList:
            if len(eachLine) > 1:
                y, x = eachLine.split(',')
                y_plot_1_1 = np.append(y_plot_1_1, [float(y)])
                x_plot_1_1 = np.append(x_plot_1_1, x)

        x_plot_1_1 = x_plot_1_1.astype("datetime64[s]")

    plot_1.clear()
    plot_1.plot_date(x_plot_1_1, y_plot_1_1, "r")


class MainWindow(tk.Tk):

    def __init__(self, *args, **kwargs):
        super().__init__()
        container = tk.Frame(self)
        tk.Tk.wm_title(self, "Test")

        container.pack(side="top", fill="both", expand=True)
        container.grid_columnconfigure(0, weight=1)
        container.grid_rowconfigure(0, weight=1)

        self.canvas = FigureCanvasTkAgg(figure_1, self)
        self.canvas.draw()
        self.canvas.get_tk_widget().pack()


if __name__ == "__main__":
    app = MainWindow()
    ani1 = animation.FuncAnimation(figure_1, animate, interval=500, save_count=0)
    app.mainloop()

0 个答案:

没有答案