为实时数据绘图实现pyqtgraph

时间:2013-07-30 15:25:28

标签: python qt qtgui pyqtgraph

我正在尝试使用pyqtgraph通过仪器收集数据的实时情节。

数据收集由主进程处理,然后通过连接传递给绘制它的子进程。我只是想传递新数据并在新数据传递时更新。

我试图通过在Qt Timer更新循环中读取连接来实现这一点,但是在连续的更新周期之间我很难传递图形数据(我想要附加)。我从下面的代码中省略了,但基本上我想加入连续的大量connData,我认为使用全局会工作,但由于某种原因我不能在这里实现。

编辑1:我与pyqtgraph示例相比的问题似乎是将数据传递给Qtimer的更新过程,我得到:

NameError:未定义全局名称“CurveData”

我试图这样做而不是使用pyqtgraph的多处理模块的原因是我不想每次都通过连接传递整个数据集(我认为这是必要的)。谢谢!

编辑2:pyqtgraph.multiprocess有哪些文档?

(这是提供的原始代码)

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

修改版本:

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 个答案:

答案 0 :(得分:7)

您尚未解释发布的示例无法解决的问题。有错误消息吗?该过程无法接收消息吗?情节结果是否与您的期望有所不同?

作为初步答案,我建议采用不同的方法:使用pyqtgraph的内置多处理功能。如果从主进程运行此代码,它将为您提供代理窗口和在新进程中运行的曲线。您在代理上调用的任何方法都将转发到远程进程:

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

请注意,我们在此处创建的所有对象(rpg,plotwin,curve和data)都是远程进程中真实对象的代理。因此,使用通常的pyqtgraph类几乎可以使用这些对象完成任何操作,结果将显示在远程进程中。例如:

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

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

这两个例子的唯一区别是起点 - 本地pyqtgraph模块的pg和远程模块的rpg。