Need single updating plot

63 views
Skip to first unread message

Chamath Vithanawasam

unread,
Feb 12, 2018, 1:42:28 AM2/12/18
to pyqtgraph
So I am new to pyqtgraph and I need to collect 20 values from a GPIO pin and then update it into a graph. This needs to keep happening repeatedly as fast as possible. I managed to get it to happen on a loop, only problem is a new plot opens every time. Any way of making sure the same plot is reused?

My code is as shown.

# Simple example of reading the MCP3008 analog input channels and printing
# them all out.
import time
import os


# Import SPI library (for hardware SPI) and MCP3008 library.
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008


# include RPi libraries in to Python code
import RPi.GPIO as GPIO
import time
from time import sleep


##BELOW ARE STUFF ASSOCIATED WITH THE PLOTTING

from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg


#-------------------------------------------#


# Software SPI configuration:
#CLK  = 18
#MISO = 23
#MOSI = 24
#CS   = 25
#mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)


# Hardware SPI configuration:
SPI_PORT  
= 0
SPI_DEVICE
= 0
mcp
= Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))


#QtGui.QApplication.setGraphicsSystem('raster')
app
= QtGui.QApplication([])
#mw = QtGui.QMainWindow()
#mw.resize(800,800)


#-------------------------------------------#

pg
.setConfigOptions(antialias=True)

def updateSensor1():
   
   
global newArray, values, data, reshaped
    newArray
= []
   
   
for x in range(20):
        values
= [0]*8
       
#for i in range(8):
       
#The read_adc function will get the value of the specified channel (0-7).
        values
[0] = mcp.read_adc(0)
       
# Print the ADC values.
               
       
print('Current value is')
       
print(values[0])
               
        newArray
.append([values[0]])
       
print(newArray)
               
        time
.sleep(0.05)


       
##ranArray = np.random
    ranArray
= np.asarray(newArray)
    data
= np.asarray(newArray)


   
print(data)


    reshaped
= np.reshape(data, (20,))
   
print(reshaped)


    p6
= pg.PlotWindow(title = "S11")
    finitecurve
= pg.PlotDataItem(reshaped, pen=(255, 255, 0))
    p6
.addItem(finitecurve)
    p6
.show()


timer1
= QtCore.QTimer()
timer1
.timeout.connect(updateSensor1)
timer1
.start(50)

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




Thanks in advance.

Carlos Pascual

unread,
Feb 12, 2018, 2:55:12 AM2/12/18
to pyqt...@googlegroups.com, 'Chamath Vithanawasam' via pyqtgraph
Have a look at the "ImageItem - Video" (ImageItem.py) example:

python -m pyqtgraph.examples



On Sunday, February 11, 2018 10:42:28 PM CET 'Chamath Vithanawasam' via
--
+----------------------------------------------------+
Carlos Pascual Izarra
Scientific Software Coordinator
Computing Division
ALBA Synchrotron [http://www.albasynchrotron.es]
Carrer de la Llum 2-26
E-08290 Cerdanyola del Valles (Barcelona), Spain
E-mail: cpas...@cells.es
Phone: +34 93 592 4428
+----------------------------------------------------+
Reply all
Reply to author
Forward
0 new messages