Advertisement
bryancole

Matplotlib GPIO thread example

May 31st, 2013
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. import matplotlib
  2. matplotlib.use("WXAgg")
  3.  
  4. from matplotlib import pyplot as plt
  5. import numpy
  6. import threading
  7. import wx
  8. import time
  9.  
  10. x = []
  11. y = []
  12.  
  13.  
  14. fig = plt.figure()
  15. ax = fig.add_subplot(111)
  16. line = ax.plot(x,y)[0]
  17. ax.set_ylim(-1,1)
  18.  
  19. def update(x, y):
  20.     """put code to update plot or other GUI stuff here"""
  21.     x = numpy.array(x)
  22.     y = numpy.array(y)
  23.     line.set_data(x, y)
  24.     fig.canvas.draw()
  25.     ax.set_xlim(x.min(), x.max())
  26.  
  27.  
  28. class ProcessThread(threading.Thread):        
  29.     def start(self):
  30.         self._running = True
  31.         super(ProcessThread, self).start()
  32.        
  33.     def stop(self):
  34.         self._running = False
  35.         self.join(5.0)
  36.        
  37.     def run(self):
  38.         """Do GPIO stuff here, but don't do GUI stuff"""
  39.         t = time.time()
  40.         while self._running:
  41.             now = time.time()
  42.             x.append(now-t)
  43.             y.append(numpy.cos(now-t))
  44.             wx.CallAfter(update, x , y) #calls update in the GUI thread
  45.             time.sleep(0.2)
  46.  
  47.  
  48. thd = ProcessThread()
  49. thd.start()
  50. plt.show()
  51. thd.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement