Advertisement
jonksar

dynamic plot

Jul 8th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. import collections
  2. import itertools
  3. import math
  4. import pyqtgraph as pg
  5. import random
  6. import time
  7. from pyqtgraph.Qt import QtGui, QtCore
  8. from threading import Thread
  9. import heapq
  10. import numpy as np
  11. import matplotlib.pyplot as mplt
  12. from rolling_window import rolling_window
  13. from ML.Models.data16Lab import *
  14. from ML.MLpreprocessing import *
  15. from bluetoothManager import OzonRing
  16.  
  17. class DynamicPlotter():
  18.  
  19. def __init__(self, sampleinterval=0.1, timewindow=10., size=(600,350), title=''):
  20. # Data stuff
  21. self._interval = int(sampleinterval*1000)
  22. self._bufsize = int(timewindow/sampleinterval)
  23. self.databuffer = collections.deque([0.0]*self._bufsize, self._bufsize)
  24. self.x = np.linspace(-timewindow, 0.0, self._bufsize)
  25. self.y = np.zeros(self._bufsize, dtype=np.float)
  26.  
  27. # PyQtGraph stuff
  28. self.app = QtGui.QApplication([])
  29. self.title = 'Dynamic Plotting with PyQtGraph' if title == '' else title
  30. self.plt = pg.plot(title=self.title)
  31. self.plt.resize(*size)
  32. self.plt.showGrid(x=True, y=True)
  33. self.plt.setLabel('left', 'amplitude', 'V')
  34. self.plt.setLabel('bottom', 'time', 's')
  35. self.curve = self.plt.plot(self.x, self.y, pen=(255,0,0))
  36.  
  37. # QTimer
  38. self.timer = QtCore.QTimer()
  39. self.timer.timeout.connect(self.updateplot)
  40. self.timer.start(self._interval)
  41.  
  42. def getdata(self):
  43. frequency = 0.5
  44. noise = random.normalvariate(0., 1.)
  45. new = 10.*math.sin(time.time()*frequency*2*math.pi) + noise
  46. return new
  47.  
  48. def updateplot(self):
  49. self.databuffer.append( self.getdata() )
  50. self.y[:] = self.databuffer
  51. self.curve.setData(self.x, self.y)
  52. self.app.processEvents()
  53.  
  54. def run(self):
  55. self.app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement