5

I am trying to get a live plot of data as it is being collected by an instrument using pyqtgraph.

The data collection is handled by the main process this is then passed over a connection to a subprocess that plots it. I am just trying to pass the new data and have it update when the new data is passed.

I have tried to implement this by putting the connection read inside the Qt Timer update loop but then I have difficulty passing the graph data (which I want to append to) between successive update cycles. I omitted from the code below, but basically I want to join up successive lots of connData, I thought using a global would work, but for some reason I can't implement that here.

EDIT 1: The problem that I have compared with the pyqtgraph examples seems to be passing data to the update process of the Qtimer, I get:

NameError: global name 'CurveData' is not defined

The reason I tried to do it this way rather than using the multiprocessing module of pyqtgraph is that I did not want to pass the entire data set over the connection each time (which I thought would be necessary). Thanks!

EDIT 2: Is there documentation for pyqtgraph.multiprocess somewhere?

(this is the original code provided)

import multiprocessing
import numpy as np
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg
import pyqtgraph.opengl as gl
import sys
import os

def MakeGraph(conn):

    win = pg.GraphicsWindow(title = "test")
    win.resize(300,300)

    p1 = win.addPlot(title = "test")

    curve = p1.plot(pen = 'y')
    timer = QtCore.QTimer()

    def Update():
        try:
            ConnData = conn.recv()
            ConnData = [float(i) for i in ConnData]
            curve.setData(ConnData)
        except EOFError:
            print "Graph connection closed\n"
            timer.stop()
            QtGui.QApplication.quit()

    timer.timeout.connect(Update)
    timer.start(0)

    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
            QtGui.QApplication.instance().exec_()

modified version:

import multiprocessing
import numpy as np
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg
import pyqtgraph.opengl as gl
import sys
import os

def MakeGraph(conn):

    win = pg.GraphicsWindow(title = "test")
    win.resize(300,300)

    p1 = win.addPlot(title = "test")

    curve = p1.plot(pen = 'y')
    timer = QtCore.QTimer()
    CurveData = []

    def Update():
        global CurveData
        try:
            ConnData = conn.recv()
            ConnData = [float(i) for i in ConnData]
            CurveData = np.append(CurveData,ConnData)
            curve.setData(CurveData)
        except EOFError:
            print "Graph connection closed\n"
            timer.stop()
            QtGui.QApplication.quit()

    timer.timeout.connect(Update)
    timer.start(0)

    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
            QtGui.QApplication.instance().exec_()

1 Answer 1

10

You have not explained what does not work about the example posted. Are there error messages? Does the process fail to receive messages? Are the plot results somehow different from what you expect?

As an initial answer, I'll recommend a different approach: use pyqtgraph's built-in multiprocessing functionality. If you run this code from your main process, it will give you a proxy to a plot window and curve running in a new process. Any methods you call on the proxies will be forwarded to the remote process:

import pyqtgraph as pg
pg.mkQApp()

# Create remote process with a plot window
import pyqtgraph.multiprocess as mp
proc = mp.QtProcess()
rpg = proc._import('pyqtgraph')
plotwin = rpg.plot()
curve = plotwin.plot(pen='y')

# create an empty list in the remote process
data = proc.transfer([])

# Send new data to the remote process and plot it
# We use the special argument _callSync='off' because we do
# not want to wait for a return value.
data.extend([1,5,2,4,3], _callSync='off')
curve.setData(y=data, _callSync='off')

Note that all of the objects we created here--rpg, plotwin, curve, and data--are proxies for real objects in the remote process. Thus almost anything you can do with the usual pyqtgraph classes can also be done with these objects and the results will appear in the remote process. For example:

# Local code:
win = pg.GraphicsWindow()
p1 = win.addPlot()
p2 = win.addPlot()

# Remote code:
win = rpg.GraphicsWindow()
p1 = win.addPlot()
p2 = win.addPlot()

The only difference for the two examples is the starting point--pg for the local pyqtgraph module, and rpg for the remote module.

5
  • Thanks, your example works for me and I can implement this into my process it also seems more elegant. However, I would like to know which of these I can call in the same way as e.g. 'pg.GraphicsWindow' in the pyqtgraph examples, if I want to add a title or more plots
    – EOF
    Jul 31, 2013 at 3:35
  • I'm having some problem with implementing your process for multiple QtProcess. I put put from proc = mp.QtProcess() ..to.. data = proc.transfer([]) into a loop so that I have a list of each of those objects. This mostly works but sometimes it stalls and I cannot ctrl-c out. Is there a more stable way to handle spawning multiple QtProcess-es? Thanks
    – EOF
    Jul 31, 2013 at 14:14
  • Sounds like a bug. As a workaround, why not just create multiple plots in a single process? That seems like a better approach anyway.
    – Luke
    Jul 31, 2013 at 18:36
  • I'm trying to run this example but I get socket.error: [Errno 48] Address already in use. Apparently the line is proc = mq.QTProcess(). @Luke Any suggestions?
    – Fra
    Mar 21, 2014 at 18:52
  • There is a recent commit in the develop branch on github that may have fixed this. Try that, and if it still broken, then post to the mailing list or open a github issue.
    – Luke
    Mar 22, 2014 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.