Skip to content

Instantly share code, notes, and snippets.

@cyan-at
Forked from Overdrivr/Plot.py
Created November 3, 2021 23:37
Show Gist options
  • Save cyan-at/edb355e63024634341c5b5c8c41c8f05 to your computer and use it in GitHub Desktop.
Save cyan-at/edb355e63024634341c5b5c8c41c8f05 to your computer and use it in GitHub Desktop.
PyQtGraph animated sine and cosine functions - real smooth
# -*- coding: utf-8 -*-
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
from numpy import arange, sin, cos, pi
import pyqtgraph as pg
import sys
class Plot2D():
def __init__(self):
self.traces = dict()
#QtGui.QApplication.setGraphicsSystem('raster')
self.app = QtGui.QApplication([])
#mw = QtGui.QMainWindow()
#mw.resize(800,800)
self.win = pg.GraphicsWindow(title="Basic plotting examples")
self.win.resize(1000,600)
self.win.setWindowTitle('pyqtgraph example: Plotting')
# Enable antialiasing for prettier plots
pg.setConfigOptions(antialias=True)
self.canvas = self.win.addPlot(title="Pytelemetry")
def start(self):
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
def trace(self,name,dataset_x,dataset_y):
if name in self.traces:
self.traces[name].setData(dataset_x,dataset_y)
else:
self.traces[name] = self.canvas.plot(pen='y')
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
p = Plot2D()
i = 0
def update():
global p, i
t = np.arange(0,3.0,0.01)
s = sin(2 * pi * t + i)
c = cos(2 * pi * t + i)
p.trace("sin",t,s)
p.trace("cos",t,c)
i += 0.1
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(50)
p.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment