小编Row*_*iek的帖子

在PyQt GUI中嵌入和更新matplotlib图时内存泄漏

我试图嵌入一个matplotlib图,每秒更新一次PyQt GUI主窗口.

在我的程序中,我threading.Timer通过timer下面显示的函数每秒调用一次更新函数.我有一个问题:我的程序每秒都会变大 - 每4秒钟大约1k.我最初的想法是append函数(返回一个新数组update_figure)不会删除旧数组?这可能是我问题的原因吗?

def update_figure(self):
    self.yAxis = np.append(self.yAxis, (getCO22()))
    self.xAxis = np.append(self.xAxis, self.i)
    # print(self.xAxis)
    if len(self.yAxis) > 10:
        self.yAxis = np.delete(self.yAxis, 0)

    if len(self.xAxis) > 10:
        self.xAxis = np.delete(self.xAxis, 0)

    self.axes.plot(self.xAxis, self.yAxis, scaley=False)
    self.axes.grid(True)

    self.i = self.i + 1

    self.draw()
Run Code Online (Sandbox Code Playgroud)

这是我的计时器功能 - 这是通过点击我的PyQt GUI中的按钮触发,然后调用自己,如您所见:

def timer(self):
    getCH4()
    getCO2()
    getConnectedDevices()
    self.dc.update_figure()
    t = threading.Timer(1.0, self.timer)
    t.start()
Run Code Online (Sandbox Code Playgroud)

编辑:我无法发布我的整个代码,因为它需要很多.dll包含.所以我将尝试解释这个程序的功能.

在我的GUI中,我想显示我的CO 2值随着时间的推移.我的get_co22函数只返回一个浮点值,我100%肯定这个工作正常.使用我的计时器,如上所示,我想继续为matplotlib图添加一个值 - 该Axes对象可供我使用self.axes.我尝试绘制数据的最后10个值.

编辑2:在聊天中进行了一些 …

python matplotlib python-3.x

5
推荐指数
1
解决办法
930
查看次数

标签 统计

matplotlib ×1

python ×1

python-3.x ×1