Python 2.7 RAM内存增加,同时使用pyQtGraph生成两个Axix图

Python 2.7 RAM内存增加,同时使用pyQtGraph生成两个Axix图,python-2.7,plot,pyqtgraph,Python 2.7,Plot,Pyqtgraph,我正在尝试绘制服务器接收到的实时数据,在这样做的同时,我的CPU(Windows XP操作系统)的RAM内存增加,我从链接“”获取了Luke campagnola所做的最新更新 任何帮助或建议都将提前得到感谢 我在这里附上整个代码 import sys from pyqtgraph.Qt import QtGui, QtCore import pyqtgraph as pg import collections import random import time import math impo

我正在尝试绘制服务器接收到的实时数据,在这样做的同时,我的CPU(Windows XP操作系统)的RAM内存增加,我从链接“”获取了Luke campagnola所做的最新更新

任何帮助或建议都将提前得到感谢

我在这里附上整个代码

import sys
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import collections
import random
import time
import math
import numpy as np
from pyqtgraph.ptime import time


class DynamicPlotter(QtGui.QWidget):   
    def __init__(self, parent = None):     
        QtGui.QWidget.__init__(self, parent) 

        sampleinterval=0.1
        self.myvar = 0
        self.prevdt=0
        self.dt1 = 0


        app = QtGui.QApplication([])

        self.plot_param1 = []
        self.plot_param2 = []
        self.plot_param3 = []
        self.plot_param4 = []
        for g in range(0,100):
            self.plot_param1.append(g/100)
            self.plot_param2 .append((g+1)/100)
            self.plot_param3.append(g/100)
            self.plot_param4.append((g+1)/100)

        self.samplesize = 100;
        self.samples     = range(0,self.samplesize)

        for i in range(len(self.samples)):
            self.samples[i] = self.samples[i]/100.0

        self.framecount = 0;

        pg.mkQApp()
        self.pw = pg.PlotWidget()

        self.pw.show()
        self.p1 = self.pw.plotItem
        self.p2 = pg.ViewBox()
        self.p1.showAxis('right')
        self.p1.scene().addItem(self.p2)
        self.p2.setGeometry(self.p1.vb.sceneBoundingRect())
        self.p1.getAxis('right').linkToView(self.p2)
        self.p2.setXLink(self.p1)

        self.pw.setLabel('bottom', 'Time in Secs')
        self.pw.setLabel('left', 'Velocity in rpm')
        self.pw.setLabel('right', 'load in Nm')

    def update(self):

        self.p1.plot(self.samples, self.plot_param3)

        self.p2.addItem(self.p1.plot(self.samples, self.plot_param4, pen='b'))  


        self.dt1  = self.dt1+1

        self.p1.setYRange(min(self.plot_param3), max(self.plot_param3))
        self.p2.setXRange(self.dt1-1,self.dt1)
        self.p2.setYRange(min(self.plot_param4), max(self.plot_param4))

        if self.framecount == 0:    
            flushloop = self.samplesize 
        else:   
            flushloop = self.samplesize+1   

        for flush in range(1,flushloop):    

            self.samples.pop(0) 


    # below code is to prepare for next sample  
    self.framecount = self.framecount + 1   
    k=0
    for update in range(self.framecount*self.samplesize,
        self.framecount*self.samplesize+self.samplesize):
        if(0):
            self.plot_param1.append(self.framecount+(update/2))
            self.plot_param2.append(self.framecount+ (update/2))
            self.myvar=self.myvar-1
        else:
            self.myvar=self.myvar+1
            #self.plot_param2.append(self.framecount+ (update/2))
            #self.plot_param1.append(self.framecount+(update/2))


        self.samples.append(update/100.0)

if(self.dt1<100):
    self.plot_param1 =  np.cos(np.linspace(0, 2*np.pi, 101))
    self.plot_param3 = self.plot_param1.tolist()
    self.plot_param2 =  np.sin(np.linspace(0, 4*np.pi, 101))
    self.plot_param4 = self.plot_param2.tolist()

if((self.dt1>=100)and (self.dt1 < 200)):
    self.plot_param1 =  np.cos(np.linspace(0, 20*np.pi, 101))
    self.plot_param3 = self.plot_param1.tolist()
    self.plot_param2 =  np.sin(np.linspace(0, 40*np.pi, 101))
    self.plot_param4 = self.plot_param2.tolist()

if((self.dt1>=200)and (self.dt1 < 300)):
    for f in range(0,100):

        self.plot_param1 =  np.cos(np.linspace(0, 20*np.pi, 101))
    self.plot_param3 = self.plot_param1.tolist()
    #self.plot_param3.append(1+f)
    self.plot_param2 =  np.cos(np.linspace(0, 20*np.pi, 101))
    #self.plot_param4.append(3+f)
    self.plot_param4 = self.plot_param2.tolist()




if(self.dt1 >= 300):
    self.plot_param1 =  np.cos(np.linspace(0, 10*np.pi, 101))
    self.plot_param3 = self.plot_param1.tolist()
    self.plot_param2 =  np.sin(np.linspace(0, 80*np.pi, 101))
    self.plot_param4 = self.plot_param2.tolist()



    for i in range (len(self.plot_param3)):

        self.plot_param3[i] = 20 * self.plot_param3[i]



if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)    
    myapp = DynamicPlotter()
    for i in range(0,100):
        myapp.update()


    timer = QtCore.QTimer()         
    timer.timeout.connect(myapp.update) 
    timer.start(50)
导入系统 从pyqtgraph.Qt导入QtGui,QtCore 将pyqtgraph作为pg导入 导入集合 随机输入 导入时间 输入数学 将numpy作为np导入 从pyqtgraph.ptime导入时间 类DynamicLotter(QtGui.QWidget): def uuu init uuu(self,parent=None): QtGui.QWidget.\uuuuu init\uuuuuu(self,parent) 采样间隔=0.1 self.myvar=0 self.prevdt=0 self.dt1=0 app=QtGui.QApplication([]) self.plot_param1=[] self.plot_param2=[] self.plot_param3=[] self.plot_param4=[] 对于范围(0100)内的g: 自绘图参数1.附加(g/100) self.plot_param2.append((g+1)/100) 自绘图参数3.附加(g/100) 自绘图参数4.追加((g+1)/100) self.samplesize=100; self.samples=范围(0,self.samplesize) 对于范围内的i(len(self.samples)): self.samples[i]=self.samples[i]/100.0 self.framecount=0; 第mkQApp页() self.pw=pg.PlotWidget() self.pw.show() self.p1=self.pw.plotItem self.p2=pg.ViewBox() self.p1.showAxis('右') self.p1.scene().addItem(self.p2) self.p2.setGeometry(self.p1.vb.sceneboundingdirect()) self.p1.getAxis('right').linkToView(self.p2) self.p2.setXLink(self.p1) self.pw.setLabel('bottom','Time in Secs') self.pw.setLabel('left','Velocity in rpm') self.pw.setLabel('right','load in Nm') def更新(自我): self.p1.绘图(self.samples,self.plot_参数3) self.p2.addItem(self.p1.plot(self.samples,self.plot_param4,pen='b')) self.dt1=self.dt1+1 self.p1.setYRange(最小值(self.plot_参数3),最大值(self.plot_参数3)) self.p2.setXRange(self.dt1-1,self.dt1) self.p2.setYRange(最小值(self.plot_参数4),最大值(self.plot_参数4)) 如果self.framecount==0: flushloop=self.samplesize 其他: flushloop=self.samplesize+1 对于范围内的冲洗(1,冲洗回路): self.samples.pop(0) #下面的代码是为下一个样本做准备 self.framecount=self.framecount+1 k=0 对于范围内的更新(self.framecount*self.samplesize, self.framecount*self.samplesize+self.samplesize): 如果(0): self.plot_param1.append(self.framecount+(更新/2)) self.plot_param2.append(self.framecount+(更新/2)) self.myvar=self.myvar-1 其他: self.myvar=self.myvar+1 #self.plot_param2.append(self.framecount+(更新/2)) #self.plot_param1.append(self.framecount+(更新/2)) self.samples.append(更新/100.0) 如果(self.dt1=100)和(self.dt1<200)): self.plot_param1=np.cos(np.linspace(0,20*np.pi,101)) self.plot_param3=self.plot_param1.tolist() self.plot_param2=np.sin(np.linspace(0,40*np.pi,101)) self.plot_param4=self.plot_param2.tolist() 如果((self.dt1>=200)和(self.dt1<300)): 对于范围(0100)内的f: self.plot_param1=np.cos(np.linspace(0,20*np.pi,101)) self.plot_param3=self.plot_param1.tolist() #self.plot_param3.append(1+f) self.plot_param2=np.cos(np.linspace(0,20*np.pi,101)) #self.plot_param4.append(3+f) self.plot_param4=self.plot_param2.tolist() 如果(self.dt1>=300): self.plot_param1=np.cos(np.linspace(0,10*np.pi,101)) self.plot_param3=self.plot_param1.tolist() self.plot_param2=np.sin(np.linspace(0,80*np.pi,101)) self.plot_param4=self.plot_param2.tolist() 对于范围内的i(len(self.plot_param3)): self.plot_param3[i]=20*self.plot_param3[i] 如果名称=“\uuuuu main\uuuuuuuu”: app=QtGui.QApplication(sys.argv) myapp=dynamicLotter() 对于范围(0100)内的i: myapp.update() 计时器=QtCore.QTimer() timer.timeout.connect(myapp.update) 定时器启动(50)
您发布的代码存在一些问题:

  • update()
    每次调用时都会向场景中添加新的打印曲线,这可能是内存使用的原因。这也会影响性能。要解决此问题,可以在
    update()的开头调用
    self.p1.clear()

  • 代码尝试创建QApplication实例至少三次,这可能导致崩溃或挂起:

    • dynamiclotter.\uuuu init\uuuu
      有一行
      app=QtGui.QApplication([])
    • dynamiclotter.\uuuu init\uuuu
      还有另一行
      pg.mkQApp()
      ,它的作用与此相同
    • 底部的
      \uuuu main\uuuu
      块也调用
      QtGui.QApplication([])
    还请注意,对于
    dynamiclotter
    的每个实例,QApplication()将被调用一次。函数
    pg.mkQApp()
    可以安全地多次调用,因为它将检查是否已创建QApplication

  • dynamiclotter
    是QWidget的一个子类,但既不显示也不用作widget。这是一个小问题,但以后可能会引起混淆


  • 我根据你的评论更新了代码,我能够解决内存问题

    当我去调试内存问题时,我还发现

    1) File PlotItem.py function addItem() has
       self.items.append(item)
       self.dataItems.append(item)
       self.curves.append(item)
    
    2) File ViewBox.py function addItem() has
       self.addedItems.append(item)
    
    上述4个列表项不断累积,不会释放以前存储的实例

    不太清楚为什么需要累积,我使用下面列出的命令删除了数组

    del self.items [0:len(self.items)]
    del self.dataItems [0:len(self.dataItems)]
    del self.curves [0:len(self.curves)]
    del self.addedItems [0:len(self.addedItems)]
    
    通过添加上述4行命令,我没有发现任何问题。你能告诉我有关情况吗

    雷加