02 September 2019

Realtime plotting in Python

If you intend to create plots/graphs with Python, your first choice may be Matplotlib. Don't use it unless your plots are extremely simple and isn't expected to grow more demanding with time. I used Matplotlib for realtime graph plotting for a really tiny graph and it was disappointingly slow. Moreover, it required installation of Python 3.6, which wasn't available readily for Ubuntu 16, and caused even more problems.

Then I came across PyQtGraph, and was impressed by the super-fast plotting speed and the mind-blowingly impressive variety of graphing functionalities. Really. You gotta see this.

Install it using: pip install pyqtgraph
Run the examples with: python3 -m pyqtgraph.examples
And prepare to be impressed!!!




I've come to understand that the rendering is quick because the underlying code is in C/C++ and the Python bindings are merely meant for invoking that code.


Still, even though the graphs are very impressive, the non-linearity of the code examples and the lack of code comments make it really hard for a newbie to understand how to get a basic graph running.

Here's where I help. After spending an extraordinarily long time figuring it out on my own, I've created a simple, basic example with which you can create a realtime graph. Hope it helps...


import time
import random
import pyqtgraph as pg
from collections import deque
from pyqtgraph.Qt import QtGui, QtCore

class Graph:
    def __init__(self, ):
        self.dat = deque()
        self.maxLen = 50#max number of data points to show on graph
        self.app = QtGui.QApplication([])
        self.win = pg.GraphicsWindow()
       
        self.p1 = self.win.addPlot(colspan=2)
        self.win.nextRow()
        self.p2 = self.win.addPlot(colspan=2)
        self.win.nextRow()
        self.p3 = self.win.addPlot(colspan=2)
       
        self.curve1 = self.p1.plot()
        self.curve2 = self.p2.plot()
        self.curve3 = self.p3.plot()
       
        graphUpdateSpeedMs = 50
        timer = QtCore.QTimer()#to create a thread that calls a function at intervals
        timer.timeout.connect(self.update)#the update function keeps getting called at intervals
        timer.start(graphUpdateSpeedMs)   
        QtGui.QApplication.instance().exec_()
       
    def update(self):
        if len(self.dat) > self.maxLen:
            self.dat.popleft() #remove oldest
        self.dat.append(random.randint(0,100)); 

        self.curve1.setData(self.dat)
        self.curve2.setData(self.dat)
        self.curve3.setData(self.dat)
        self.app.processEvents()  
       

if __name__ == '__main__':
    g = Graph()

   
    
 
________________________________


The fact that you are here means that you use the computer a lot. So please also take some time to understand the real cure for eye strain. Yes, it matters.