|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Copyright (C) 2003-2004 Andrew Straw, Jeremy O'Donoghue and others |
| 4 | + |
| 5 | +License: This work is licensed under the PSF. A copy should be included |
| 6 | +with this source code, and is also available at |
| 7 | +http://www.python.org/psf/license.html |
| 8 | +
|
| 9 | +This is yet another example of using matplotlib with wx. Hopefully |
| 10 | +this is pretty full-featured: |
| 11 | +
|
| 12 | + - both matplotlib toolbar and WX buttons manipulate plot |
| 13 | + - full wxApp framework, including widget interaction |
| 14 | + - XRC (XML wxWidgets resource) file to create GUI (made with XRCed) |
| 15 | +
|
| 16 | +This was derived from embedding_in_wx and dynamic_image_wxagg. |
| 17 | +
|
| 18 | +Thanks to matplotlib and wx teams for creating such great software! |
| 19 | +
|
| 20 | +""" |
| 21 | +import sys, time, os, gc |
| 22 | +import matplotlib |
| 23 | +matplotlib.use('WXAgg') |
| 24 | +from matplotlib import rcParams |
| 25 | +rcParams['image.origin'] = 'upper' # 'lower': nav toolbar problem -ADS |
| 26 | +import matplotlib.cm as cm |
| 27 | +from matplotlib.backends.backend_wxagg import Toolbar, FigureCanvasWxAgg |
| 28 | +from matplotlib.figure import Figure |
| 29 | +import matplotlib.numerix as numerix |
| 30 | +from wxPython.wx import * |
| 31 | +from wxPython.xrc import * |
| 32 | + |
| 33 | +ERR_TOL = 1e-5 # floating point slop for peak-detection |
| 34 | + |
| 35 | +class PlotPanel(wxPanel): |
| 36 | + |
| 37 | + def __init__(self, parent): |
| 38 | + wxPanel.__init__(self, parent, -1) |
| 39 | + |
| 40 | + self.fig = Figure((5,4), 75) |
| 41 | + self.canvas = FigureCanvasWxAgg(self, -1, self.fig) |
| 42 | + self.toolbar = Toolbar(self.canvas) #matplotlib toolbar |
| 43 | + self.toolbar.Realize() |
| 44 | + #self.toolbar.set_active([0,1]) |
| 45 | + |
| 46 | + # Now put all into a sizer |
| 47 | + sizer = wxBoxSizer(wxVERTICAL) |
| 48 | + # This way of adding to sizer allows resizing |
| 49 | + sizer.Add(self.canvas, 1, wxLEFT|wxTOP|wxGROW) |
| 50 | + # Best to allow the toolbar to resize! |
| 51 | + sizer.Add(self.toolbar, 0, wxGROW) |
| 52 | + self.SetSizer(sizer) |
| 53 | + self.Fit() |
| 54 | + |
| 55 | + def init_plot_data(self): |
| 56 | + a = self.fig.add_subplot(111) |
| 57 | + self.x = numerix.arange(120.0)*2*numerix.pi/60.0 |
| 58 | + self.x.resize((100,120)) |
| 59 | + self.y = numerix.arange(100.0)*2*numerix.pi/50.0 |
| 60 | + self.y.resize((120,100)) |
| 61 | + self.y = numerix.transpose(self.y) |
| 62 | + z = numerix.sin(self.x) + numerix.cos(self.y) |
| 63 | + self.im = a.imshow( z, cmap=cm.jet)#, interpolation='nearest') |
| 64 | + |
| 65 | + zmax = numerix.max(numerix.max(z))-ERR_TOL |
| 66 | + ymax_i, xmax_i = numerix.nonzero( |
| 67 | + numerix.greater_equal(z, zmax)) |
| 68 | + if self.im.origin == 'upper': |
| 69 | + ymax_i = z.shape[0]-ymax_i |
| 70 | + self.lines = a.plot(xmax_i,ymax_i,'ko') |
| 71 | + |
| 72 | + self.toolbar.update() # Not sure why this is needed - ADS |
| 73 | + |
| 74 | + def GetToolBar(self): |
| 75 | + # You will need to override GetToolBar if you are using an |
| 76 | + # unmanaged toolbar in your frame |
| 77 | + return self.toolbar |
| 78 | + |
| 79 | + def OnWhiz(self,evt): |
| 80 | + self.x += numerix.pi/15 |
| 81 | + self.y += numerix.pi/20 |
| 82 | + z = numerix.sin(self.x) + numerix.cos(self.y) |
| 83 | + self.im.set_array(z) |
| 84 | + |
| 85 | + zmax = numerix.max(numerix.max(z))-ERR_TOL |
| 86 | + ymax_i, xmax_i = numerix.nonzero( |
| 87 | + numerix.greater_equal(z, zmax)) |
| 88 | + if self.im.origin == 'upper': |
| 89 | + ymax_i = z.shape[0]-ymax_i |
| 90 | + self.lines[0].set_data(xmax_i,ymax_i) |
| 91 | + |
| 92 | + self.canvas.draw() |
| 93 | + |
| 94 | + def onEraseBackground(self, evt): |
| 95 | + # this is supposed to prevent redraw flicker on some X servers... |
| 96 | + pass |
| 97 | + |
| 98 | +class MyApp(wxApp): |
| 99 | + def OnInit(self): |
| 100 | + self.res = wxXmlResource("data/embedding_in_wx3.xrc") |
| 101 | + |
| 102 | + # main frame and panel --------- |
| 103 | + |
| 104 | + self.frame = self.res.LoadFrame(None,"MainFrame") |
| 105 | + self.panel = XRCCTRL(self.frame,"MainPanel") |
| 106 | + |
| 107 | + # matplotlib panel ------------- |
| 108 | + |
| 109 | + # container for matplotlib panel (I like to make a container |
| 110 | + # panel for our panel so I know where it'll go when in XRCed.) |
| 111 | + plot_container = XRCCTRL(self.frame,"plot_container_panel") |
| 112 | + sizer = wxBoxSizer(wxVERTICAL) |
| 113 | + |
| 114 | + # matplotlib panel itself |
| 115 | + self.plotpanel = PlotPanel(plot_container) |
| 116 | + self.plotpanel.init_plot_data() |
| 117 | + |
| 118 | + # wx boilerplate |
| 119 | + sizer.Add(self.plotpanel, 1, wxEXPAND) |
| 120 | + plot_container.SetSizer(sizer) |
| 121 | + |
| 122 | + # whiz button ------------------ |
| 123 | + |
| 124 | + whiz_button = XRCCTRL(self.frame,"whiz_button") |
| 125 | + EVT_BUTTON(whiz_button, whiz_button.GetId(), |
| 126 | + self.plotpanel.OnWhiz) |
| 127 | + |
| 128 | + # bang button ------------------ |
| 129 | + |
| 130 | + bang_button = XRCCTRL(self.frame,"bang_button") |
| 131 | + EVT_BUTTON(bang_button, bang_button.GetId(), |
| 132 | + self.OnBang) |
| 133 | + |
| 134 | + # final setup ------------------ |
| 135 | + |
| 136 | + sizer = self.panel.GetSizer() |
| 137 | + self.frame.Show(1) |
| 138 | + |
| 139 | + self.SetTopWindow(self.frame) |
| 140 | + |
| 141 | + return True |
| 142 | + |
| 143 | + def OnBang(self,event): |
| 144 | + bang_count = XRCCTRL(self.frame,"bang_count") |
| 145 | + bangs = bang_count.GetValue() |
| 146 | + bangs = int(bangs)+1 |
| 147 | + bang_count.SetValue(str(bangs)) |
| 148 | + |
| 149 | +if __name__ == '__main__': |
| 150 | + app = MyApp(0) |
| 151 | + app.MainLoop() |
| 152 | + |
0 commit comments