How can I plot a deque whenever it gets a value?

53 views
Skip to first unread message

clouda...@gmail.com

unread,
Jul 16, 2017, 10:32:45 AM7/16/17
to pyqtgraph

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()




>>> (executing file "<tmp 1>")

Plotter received: 1

deque([1])

Plotter received: 2

deque([1, 2])

Plotter received: 3

deque([1, 2, 3])

Plotter received: 4

deque([1, 2, 3, 4])

Plotter received: 5

deque([1, 2, 3, 4, 5])


vas...@gmail.com

unread,
Jul 16, 2017, 1:14:57 PM7/16/17
to pyqt...@googlegroups.com
Try to QtGui.QApplication.processEvents() after self.curve_1.setData(self.deq):
    def plot(self, msg):
        print('Plotter received: ', msg)
        self.deq.append(msg)
        print(self.deq)
        self.curve_1.setData(self.deq)
        QtGui.QApplication.processEvents()


--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/912108af-e817-4183-a4f8-8763fb502379%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

clouda...@gmail.com

unread,
Jul 17, 2017, 7:39:38 PM7/17/17
to pyqtgraph
Thank you so much! It works now! Have a good day! :)
Reply all
Reply to author
Forward
0 new messages