This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Matplotlib GPIO thread example

By: bryancole on May 31st, 2013  |  syntax: Python  |  size: 1.08 KB  |  views: 69  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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()
clone this paste RAW Paste Data