1
0

hellomatplotlib.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env python
  2. # This example was adapted from http://matplotlib.org/examples/user_interfaces/embedding_in_gtk.html, and http://pygtk.org/pygtk2tutorial/examples/helloworld.py
  3. import pygtk
  4. pygtk.require('2.0')
  5. import gtk
  6. from matplotlib.figure import Figure
  7. from numpy import arange, sin, pi
  8. from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
  9. class HelloMatplotlib:
  10. def __init__(self):
  11. self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  12. self.window.connect("delete_event", self.delete_event)
  13. self.window.connect("destroy", self.destroy)
  14. self.window.set_size_request(400, 400)
  15. self.window.set_border_width(10)
  16. f = Figure(figsize=(5,4), dpi=100)
  17. a = f.add_subplot(111)
  18. t = arange(0.0,3.0,0.01)
  19. s = sin(2*pi*t)
  20. a.plot(t,s)
  21. self.canvas = FigureCanvas(f)
  22. self.canvas.show()
  23. self.window.add(self.canvas)
  24. self.window.show()
  25. def delete_event(self, widget, event, data=None):
  26. gtk.main_quit()
  27. def destroy(self, widget, data=None):
  28. gtk.main_quit()
  29. def main(self):
  30. gtk.main()
  31. def main():
  32. hello = HelloMatplotlib()
  33. hello.main()