嵌入Wx2

如何在具有新工具栏的应用程序中使用wxagg的示例

  1. from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
  2. from matplotlib.backends.backend_wx import NavigationToolbar2Wx as NavigationToolbar
  3. from matplotlib.figure import Figure
  4. import numpy as np
  5. import wx
  6. import wx.lib.mixins.inspection as WIT
  7. class CanvasFrame(wx.Frame):
  8. def __init__(self):
  9. wx.Frame.__init__(self, None, -1,
  10. 'CanvasFrame', size=(550, 350))
  11. self.figure = Figure()
  12. self.axes = self.figure.add_subplot(111)
  13. t = np.arange(0.0, 3.0, 0.01)
  14. s = np.sin(2 * np.pi * t)
  15. self.axes.plot(t, s)
  16. self.canvas = FigureCanvas(self, -1, self.figure)
  17. self.sizer = wx.BoxSizer(wx.VERTICAL)
  18. self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.EXPAND)
  19. self.SetSizer(self.sizer)
  20. self.Fit()
  21. self.add_toolbar() # comment this out for no toolbar
  22. def add_toolbar(self):
  23. self.toolbar = NavigationToolbar(self.canvas)
  24. self.toolbar.Realize()
  25. # By adding toolbar in sizer, we are able to put it at the bottom
  26. # of the frame - so appearance is closer to GTK version.
  27. self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
  28. # update the axes menu on the toolbar
  29. self.toolbar.update()
  30. # alternatively you could use
  31. #class App(wx.App):
  32. class App(WIT.InspectableApp):
  33. def OnInit(self):
  34. 'Create the main window and insert the custom frame'
  35. self.Init()
  36. frame = CanvasFrame()
  37. frame.Show(True)
  38. return True
  39. app = App(0)
  40. app.MainLoop()

下载这个示例