Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Apr 1st, 2013  |  syntax: Python  |  size: 1.18 KB  |  views: 178  |  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 serial
  2. import numpy as np
  3.  
  4. from matplotlib.figure import Figure
  5. from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas
  6. from gi.repository import Gtk
  7. import time
  8. import gobject
  9.  
  10. class MyWindow(Gtk.ApplicationWindow):
  11.     def __init__(self):
  12.  
  13.         Gtk.Window.__init__(self, title="Toolbar Example")
  14.         self.set_size_request(700, 500)
  15.         self.box = Gtk.Box(spacing=6, orientation=Gtk.Orientation.VERTICAL)
  16.         self.add(self.box)
  17.  
  18.         fig = Figure()
  19.         ax = fig.add_subplot(111)
  20.         ax.set_xlim(0, 50)
  21.         ax.set_ylim(0, 20)
  22.  
  23.         ydata = [0] * 50
  24.         line, = ax.plot(ydata, label='ydata')
  25.         ax.legend()
  26.  
  27.         f = FigureCanvas(fig)
  28.         self.box.pack_start(f, True, True, 0)
  29.        
  30.         def update_graph():
  31.                 data = ser.readline().rstrip()
  32.                 if len(data.split(".")) == 2:
  33.                         print data
  34.                         ymin = float(min(ydata))-10
  35.                         ymax = float(max(ydata))+10
  36.                         ax.set_ylim(ymin, ymax)
  37.                         ydata.append(data)
  38.                         del ydata[0]
  39.                         line.set_data(np.arange(len(ydata)), ydata)
  40.                         f.draw()
  41.                 return True
  42.  
  43.         gobject.idle_add(update_graph)
  44.        
  45. ser = serial.Serial('/dev/ttyACM0', 9600)
  46. win = MyWindow()
  47. win.connect("delete-event", Gtk.main_quit)
  48. win.show_all()
  49.  
  50. Gtk.main()
clone this paste RAW Paste Data