#!/usr/bin/env python
"""
show how to add a matplotlib FigureCanvasGTK or FigureCanvasGTKAgg widget and
a toolbar to a gtk.Window
"""
import math
import gobject
from pylab import *
rcParams['numerix'] = 'numpy'
import matplotlib
matplotlib.use('GTK')
from matplotlib.figure import Figure
from matplotlib.axes import Subplot
from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas
from matplotlib.backends.backend_gtk import NavigationToolbar2GTK as NavigationToolbar
try:
import pygtk
pygtk.require("2.0")
except:
pass
try:
import gtk
import gtk.glade
except:
sys.exit(1)
TIME = range(360)
VOLT1 = [math.sin(math.radians(x+0)) for x in TIME]
VOLT2 = [math.sin(math.radians(x+120)) for x in TIME]
VOLT3 = [math.sin(math.radians(x+240)) for x in TIME]
PLOT_DATA = [VOLT1,VOLT2,VOLT3]
###################################################################
class appGui:
def __init__(self):
self.gladefile = 'graph-gui.glade'
self.windowname = 'graph-it'
self.wTree = gtk.glade.XML(self.gladefile, self.windowname)
dic = {"on_quit_clicked" : self.QuitClicked,##callback dictionary
"on_plot_clicked" : self.Plot,
"on_window_destroy" : (gtk.main_quit)}
self.wTree.signal_autoconnect(dic)
#self.figure = Figure(figsize=(6,4), dpi=100)
#self.axis = self.figure.add_subplot(111)
def test_timeout(self):
print "testing"
return True
gobject.timeout_add(1000,test_timeout,self)
def QuitClicked(self,widget):
gtk.main_quit()
def Plot(self,widget):
try:
self.canvas.destroy()
self.toolbar.destroy()
except:pass
self.figure = Figure(figsize=(6,4), dpi=100)
self.axis = self.figure.add_subplot(111)
self.axis.grid(True)
self.axis.set_ylabel('Volts (V)')
self.axis.set_xlabel('Time (s)')
self.axis.set_title('Test Graph')
for x in ['signal1','signal2','signal3']:
tmp = self.wTree.get_widget(x).get_active()
if tmp == -1: continue
self.axis.plot(TIME,PLOT_DATA[tmp])
#self.axis.plot(TIME,VOLT1,'r')
#self.axis.plot(TIME,VOLT2,'y')
#self.axis.plot(TIME,VOLT3,'b')
self.canvas = FigureCanvas(self.figure) # a gtk.DrawingArea
self.canvas.show()
self.graphview = self.wTree.get_widget("vbox1")
self.graphview.pack_start(self.canvas, True, True)
self.toolbar = NavigationToolbar(self.canvas,self.wTree.get_widget('graph-it'))
self.graphview.pack_start(self.toolbar, False, False)
if __name__ == '__main__':
app=appGui()
gtk.main()