How can I plot a deque whenever it gets a value, using pyqtgraph and pypubsub?

788 Views Asked by At

I'm trying to make a program in which an instance sends values to another instance and it plots them continuously. I programmed this using pypubsub to send values from one instance to the other. The other instance gets values and store them into a deque, and it plots the deque whenever it is updates.

I think the instances communicate with one another well and I can see the deque is updated every second as I planned, however, the problem is that the graph doesn't show the deque's values whenever it is updated, but rather it shows the values once the whole updating has finished. I'd like to know how I can plot the deque whenever it is updated.

from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg

from collections import deque
from pubsub import pub
import time 


class Plotter:
    def __init__(self):

        self.deq = deque()

        self.pw = pg.GraphicsView()
        self.pw.show()
        self.mainLayout = pg.GraphicsLayout()
        self.pw.setCentralItem(self.mainLayout)
        self.p1 = pg.PlotItem()       
        self.p1.setClipToView=True
        self.curve_1 = self.p1.plot(pen=None, symbol='o', symbolPen=None, symbolSize=10, symbolBrush=(102, 000, 000, 255))
        self.mainLayout.addItem(self.p1, row = 0, col=0, rowspan=2)                         

    def plot(self, msg):
        print('Plotter received: ', msg)
        self.deq.append(msg)
        print(self.deq)
        self.curve_1.setData(self.deq)


class Sender:
    def __init__(self):
        self.list01 = [1,2,3,4,5]            # A list of values that will be sent through pub.sendMessage

    def send(self):
        for i in range(len(self.list01)):
            pub.sendMessage('update', msg = self.list01[i] )        
            time.sleep(1)


plotterObj = Plotter()    
senderObj = Sender()

pub.subscribe(plotterObj.plot, 'update')

senderObj.send()
1

There are 1 best solutions below

0
On

Looking at the sendmessage and subscribe, all looks good. But I notice you don't have a QApplication instance and an event loop. Create the app, and call exec() at the end so it enters event loop. Rendering will occur then.

app = QtGui.QApplication([])

plotterObj = Plotter()
senderObj = Sender()

pub.subscribe(plotterObj.plot, 'update')

senderObj.send()

app.exec()