Skip to content

Instantly share code, notes, and snippets.

@eyllanesc
Created December 2, 2018 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyllanesc/1980606ba040013b847a96f8cdb6af02 to your computer and use it in GitHub Desktop.
Save eyllanesc/1980606ba040013b847a96f8cdb6af02 to your computer and use it in GitHub Desktop.
from PyQt5 import QtCore, QtWidgets
import queue
import threading
import pyqtgraph as pg
import numpy as np
class App(QtWidgets.QMainWindow):
def __init__(self):
super(App, self).__init__()
self.initUI()
# define plots axis and labels
self.pc1 = pg.PlotDataItem(name="ACC")
self.pc2 = pg.PlotDataItem(name="ACC2")
self.pc3 = pg.PlotDataItem(name="ACC3")
self.pw1.addItem(self.pc1)
self.pw1.addItem(self.pc2)
self.pw1.addItem(self.pc3)
self.pw1.setLabel('left', 'LEFT FOOT ACC', units='m/s^2')
self.pw1.setLabel('bottom', 'Time', units='s')
# timer
self.timer = QtCore.QTimer(self, timeout=self.updateplot)
self.data_out = []
self.emulate_data()
def emulate_data(self):
self.l_queue = queue.Queue(10)
def io(q):
t = 0
while True:
r = np.cos(2 * np.pi *t)
s = np.sin(2 * np.pi *t)
u = np.exp(-2 * np.pi * t)
t += 0.01
q.put([t, r, s, u, r*s])
QtCore.QThread.msleep(10)
print("Done")
t = threading.Thread(target=io, args=(self.l_queue,), daemon=True)
t.start()
def initUI(self):
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
grid_layout = pg.QtGui.QGridLayout(central_widget)
# create subplot grid
self.pw1 = pg.PlotWidget()
grid_layout.addWidget(self.pw1, 2, 0, 1, 4)
# create start/stop button
self.on_start = QtWidgets.QPushButton("START", clicked=self.on_start_clicked)
self.on_stop = QtWidgets.QPushButton("STOP")
grid_layout.addWidget(self.on_start,4,0,1,8)
grid_layout.addWidget(self.on_stop,5,0,1,8)
def on_start_clicked(self):
# do something
if not self.timer.isActive():
self.timer.timeout.connect(self.updateplot)
self.timer.start(8)
def on_stop_clicked(self):
# do something
pass
def updateplot(self):
while not self.l_queue.empty():
self.data_out.append(self.l_queue.get())
if self.data_out:
self.pc1.setData(np.array(self.data_out)[:,2], pen=(255,0,0))
self.pc2.setData(np.array(self.data_out)[:,3], pen=(0,255,0))
self.pc3.setData(np.array(self.data_out)[:,4], pen=(0,0,255))
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())
@eyllanesc
Copy link
Author

screenshot from 2018-12-01 22-27-05

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment