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.
I know it is a silly question. I am a beginner in Python and coding.
Here is my code:
#-*- 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 -*-
I would really appreciate your help.
matplotlib: For plotting, pyqtgraph is not nearly as complete/mature as matplotlib, but runs much faster. Matplotlib is more aimed toward making publication-quality graphics, whereas pyqtgraph is intended for use in data acquisition and analysis applications.
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.
When you call:
p1 = win.addPlot()
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 )
So when you call:
curve1 = p1.plot()
This adds PlotDataItem #1, ... you now need to call it again to get a second reference to use:
curve2 = p1.plot()
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'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With