Search code examples
python-2.7plotpyqtgraph

Realtime plot with pyqtgraph


I have a problem regarding real-time plotting with pyqtgraph. I want the plot to update itself every 100 items collected from serial input, but the curve shows only once, after gathering the data is finished. Debugging print "boo" gets printed to console after every 100 items, but updatePlot() seems to be called only while ending the loop. This is my code:

class EKG(QtGui.QMainWindow, out.Ui_MainWindow):

    def __init__(self):
        super(self.__class__, self).__init__()
        self.setupUi(self)
        self.collectedData = []
        self.dataPlot.showGrid(x=True, y=True, alpha=0.6)
        self.plt = self.dataPlot.plot(pen='m', antialias=True)
        self.port = "COM9"
        self.actionZako_cz.triggered.connect(self.close_window)
        self.startBtn.clicked.connect(self.collectData)

    def getTime(self):
        return int(self.timeBox.toPlainText())

    def updatePlot(self):
        self.plt.setData(self.collectedData)

    def collectData(self, howLong):
        howLong = self.getTime()
        self.collectedData = []
        serialData = serial.Serial(self.port, 57600)
        t_end = time.time() + howLong
        while time.time() < t_end:
            try:
                self.collectedData.append(int(serialData.readline().strip()))
            except ValueError:
                pass
            if len(self.collectedData) % 100 == 0:
                print "boo"
                self.updatePlot()
        serialData.close()

I would be grateful for any advice; it's the first time i'm using pyqtgraph and I haven't got the hang of it yet...


Solution

  • I've came across the solution, in case anybody stumbles upon a similar problem:

    def updatePlot(self):
        self.plt.setData(self.collectedData)
        QtGui.QApplication.processEvents()
    

    adding a call to process events causes the plot to update properly!