重新启动实时绘图时,pyqtgraph实时绘图中断

Posted

技术标签:

【中文标题】重新启动实时绘图时,pyqtgraph实时绘图中断【英文标题】:pyqtgraph realtime plot broke when re-start the realtime plot 【发布时间】:2019-02-13 09:20:33 【问题描述】:

所以,在这个程序中,我可以通过更改切片器值来启动 Qthread 实时绘图,当我将滑块值设置为零时,QThread 实时绘图会停止绘图。 但有时当我尝试再次重新启动实时绘图时,情节看起来像这张图片一样在短时间内被破坏了。

在这张图片中,实际折线图下方有一个单线斜线

这是我的代码:

ui.py

    from pyqtgraph.Qt import QtCore, QtGui
from PyQt4 import QtCore,QtGui,uic
import sys
import time
from collections import deque
from DynamicThread import DynamicThread
from ThreadRun import ThreadRun

from GUI_ui import Ui_home

class ui(QtGui.QTabWidget):
    akselerasi = 0
    curve = None
    dataGraph = deque(maxlen=20)
    x1 = []
    y1 = []
    aa = False

    def __init__(self,parent = None):
        super(ui,self).__init__(parent = parent)

        self.UiMeja = Ui_home()
        self.UiMeja.setupUi(self)

        self.UiMeja.akselerasi.setMinimum(0)
        self.UiMeja.akselerasi.setMaximum(50)
        self.UiMeja.akselerasi.setValue(0)
        self.UiMeja.akselerasi.valueChanged.connect(self.valueAkselerasi)
        self.UiMeja.akselerasi.sliderReleased.connect(self.getThread)

        self.threadDinamis = DynamicThread()
        self.threadDinamis.dataThread.connect(self.runningDynamic)
        ui.curve = self.UiMejaGetar.graph1.plot(pen=(255,0,0))
        self.UiMeja.graph1.setRange(xRange=None, yRange=[-1, 1 ])
        self.UiMeja.graph1.showGrid(x=True, y=True)
        self.UiMeja.graph1.setMouseEnabled(x=False, y=False)
        self.UiMejaGetar.l1.setText(str(self.UiMejaGetar.akselerasi.value()))

    def getThread(self):

        if self.sender() == self.UiMeja.akselerasi:
            ui.akselerasii = int(self.UiMeja.akselerasi.value())

        if GUI_MejaGetar.akselerasii > 0:
            if ui.aa == True:
                print("second")
                minRange = int(self.UiMeja.akselerasi.value()) * -1
                self.UiMeja.graph1.setRange(xRange=None, yRange=[minRange, int(self.UiMeja.akselerasi.value()) ])
                self.threadDinamis.trigrer(ui.akselerasii)

            else:
                minRange = int(self.UiMeja.akselerasi.value()) * -1
                self.UiMeja.graph1.setRange(xRange=None, yRange=[minRange, int(self.UiMeja.akselerasi.value()) ])
                GUI_MejaGetar.aa = True

                print("first")
                self.threadDinamis.trigrer(GUI_MejaGetar.akselerasii)
                self.threadDinamis.start()
        else:
            self.threadDinamis.stop()
            GUI_MejaGetar.aa = False
            GUI_MejaGetar.dataGraph.clear()
            GUI_MejaGetar.x1 = []
            GUI_MejaGetar.y1 = []

    def runningDynamic(self,generateData):
        #print(generateData['x'])
        GUI_MejaGetar.dataGraph.append('x': generateData['x'], 'y': generateData['y'])
        GUI_MejaGetar.x1 = [item['x'] for item in GUI_MejaGetar.dataGraph]
        GUI_MejaGetar.y1 = [item['y'] for item in GUI_MejaGetar.dataGraph]
        GUI_MejaGetar.curve.setData(x=GUI_MejaGetar.x1, y=GUI_MejaGetar.y1)


if __name__ == '__main__':

    app = QtGui.QApplication([])
    widget = GUI_MejaGetar()

    widget.show()
    app.exec_()

这是我的线程代码:

动态线程.py

    import time
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QTime, QTimer
from randomMethod import randomMethod

class DynamicThread(QtCore.QThread):
    dataThread = QtCore.pyqtSignal(object)
    dynamicGenerator = randomMethod()
    status = False
    intAkselerasi = 0
    def __init__(self, parent=None):
        QtCore.QThread.__init__(self,parent)
        #self.gui_text = None

    def trigrer(self,num):
        DynamicThread.intAkselerasi = num

    def do_work(self):
        i = 0
        listData = []
        count = 0
        test = 0
        #t = QTime()
        #t.start()
        print("starting.....")

        while DynamicThread.status == True:
            if DynamicThread.intAkselerasi > 0:
                if not listData:
                    value = int(DynamicThread.dynamicGenerator.bits(), 2)
                    dat = value % DynamicThread.intAkselerasi
                    listData.append(dat)
                    count = dat
                else:
                    value = int(DynamicThread.dynamicGenerator.bits(), 2)
                    dat = value % DynamicThread.intAkselerasi

                    if listData[i - 1] < 0:
                        dat = dat * -1
                    test = 0
                    test = count + dat
                    lowLimit = DynamicThread.intAkselerasi * -1
                    if DynamicThread.intAkselerasi > test > lowLimit:
                        count = count + dat
                        listData.append(count)
                    else:
                        if dat < lowLimit:
                            temp = abs(dat)
                            count = count + temp
                            listData.append(temp)
                        else:
                            temp = dat * -1
                            count = count + temp
                            listData.append(temp)
                print(listData[i])
                print("test: ",test)
                print("count: ",count)
                self.dataThread.emit('x': i,'y':count)
                i = i + 1
                time.sleep(0.1)
            else:
                DynamicThread.status = False
                print("stop")
                break
        print("stoping the process....")

    def run(self):
        self.do_work()

    def start(self):
        DynamicThread.status = True
        super(DynamicThread, self).start()

    def stop(self):
        DynamicThread.status = False

那么,我的代码有什么问题?知道如何解决吗?谢谢 (注:我在树莓派 3 上运行此代码)

【问题讨论】:

【参考方案1】:

为了解决这个问题,我添加了

GUI_MejaGetar.dataGraph.clear()
GUI_MejaGetar.x1 = []
GUI_MejaGetar.y1 = []

每次我开始第一次或结束实时循环时

【讨论】:

以上是关于重新启动实时绘图时,pyqtgraph实时绘图中断的主要内容,如果未能解决你的问题,请参考以下文章

为实时数据绘图实现 pyqtgraph

即使设备断开连接,使用 PyQtGraph 的实时绘图仍在绘制

在 PyQt4 中使用 PyQtGraph 进行实时绘图

pyQtgraph 绘图延迟串行绘图

在pyqtgraph中实现实时绘图的最简单方法是啥

在 PyQt4 #2 中使用 PyQtGraph 进行实时绘图