0

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()
Oliver
  • 27,510
  • 9
  • 72
  • 103
maynull
  • 1,936
  • 4
  • 26
  • 46
  • Please simplify your question and example - weed out all the stuff that works. You are trying to update a graph when the underlying data changes. Focus on what your problem is. Write a short script that encapsulates your problem - with some fake data; iterate over the data and *update* a graph. [mcve] – wwii Jul 16 '17 at 13:45
  • @wwii I'm sorry! Since English is not my mother tongue, it may sound incoherent. And it was not easy to explain the problem when it's related to graphic. – maynull Jul 16 '17 at 13:51
  • Are you using pubsub from github/schollii/pypubsub? – Oliver Jul 19 '17 at 02:28

1 Answers1

0

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()
Oliver
  • 27,510
  • 9
  • 72
  • 103