如何在 PyQtGraph 的一个图中绘制两个实时数据? [英] How to plot two real-time data in one single plot in PyQtGraph?

查看:49
本文介绍了如何在 PyQtGraph 的一个图中绘制两个实时数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我愿意使用 PyQtGraph 以实时方式获取 2 个随机数据并将其绘制在同一个 Widget 中.我希望它们显示为红点和蓝点.但是,经过一段时间后,我的脚本不起作用.

I am willing to get 2 random data and plot it in the same Widget using PyQtGraph in a real-time way. I want them to show up as Red and Blue dots. However, after a hard time, my script does not work.

我想知道我该怎么做才能在同一个图中获得两个数据.

I would like to know what can I do in order to get both data in the same plot.

我知道这是一个愚蠢的问题.我是 Python 和编码的初学者.

I know it is a silly question. I am a beginner in Python and coding.

这是我的代码:

#-*- coding: utf-8 -*-
import random
import time
from collections import deque
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
import os
import spidev

win = pg.GraphicsWindow()
win.setWindowTitle('DOTS')


p1 = win.addPlot()
p1.setRange(yRange=[0,25])
p1.setRange(xRange=[0,25])
curve1 = p1.plot()


nsamples=300 #Number of lines for the data

dataRed= np.zeros((nsamples,2),float) #Matrix for the Red dots
dataBlue=np.zeros((nsamples,2),float) #Matrix for the Blue dots

def getData():
    global dataRed, dataBlue

    t0= random.uniform(1.6,20.5) #Acquiring Data
    d0= random.uniform(1.6,20.5) #Acquiring Data
    vec=(t0, d0)

    dataRed[:-1] = dataRed[1:]
    dataRed[-1]=np.array(vec)

    t0= random.uniform(1.6,20.5) #Acquiring Data
    d0= random.uniform(1.6,20.5) #Acquiring Data
    vec=(t0, d0)

    dataBlue[:-1] = dataBlue[1:]
    dataBlue[-1]=np.array(vec)


def plot():

    #Blue Dots
    curve1.setData(dataBlue, pen=None, symbol='o', symbolPen=None, symbolSize=4, symbolBrush=('b'))
    #Red Dots
    curve1.setData(dataRed, pen=None, symbol='o', symbolPen=None, symbolSize=4, symbolBrush=('r'))   


def update():

    getData()
    plot()

timer = pg.QtCore.QTimer()
timer.timeout.connect(update)
timer.start(50)

## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()# -*- coding: utf-8 -*-

非常感谢您的帮助.

推荐答案

虽然从功能的角度来看 pyqtgraph 是一个很棒的包,但不幸的是缺少文档,你真的只需要深入代码并开始理解对象的结构.

While pyqtgraph is a great package from a functionality perspective, unfortunately the documentation is lacking, and you really just have to dig in to the code and start to understand the structure of the objects.

当你打电话时:

p1 = win.addPlot() 

这将返回对 PlotItem 的引用,此时您现在可以向该 p1 对象添加多个 PlotDataItem(请参阅此处的半有用结构图:http://www.pyqtgraph.org/documentation/plotting.html#organization-of-plotting-classes )

This returns a reference to a PlotItem, at which point you can now add multiple PlotDataItems to this p1 object (see semi-useful structure diagram here : http://www.pyqtgraph.org/documentation/plotting.html#organization-of-plotting-classes )

所以当你打电话时:

curve1 = p1.plot()

这将添加 PlotDataItem #1,...您现在需要再次调用它以获取第二个要使用的引用:

This adds PlotDataItem #1, ... you now need to call it again to get a second reference to use:

curve2 = p1.plot()

这将成为 PlotDataItem #2,然后您可以将其用于 plot() 方法中的第二个 setData 方法,以便在 update() 期间调用.看起来像:

This becomes PlotDataItem #2, which you can then use for the 2nd setData method in your plot() method to call during update(). Which would look like:

def plot():

    #Blue Dots
    curve1.setData(dataBlue, pen=None, symbol='o', symbolPen=None, symbolSize=4, symbolBrush=('b'))
    #Red Dots
    curve2.setData(dataRed, pen=None, symbol='o', symbolPen=None, symbolSize=4, symbolBrush=('r')) 

这篇关于如何在 PyQtGraph 的一个图中绘制两个实时数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆