|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Copyright (C) 2003-2004 Jeremy O'Donoghue, Andrew Straw 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 | +Modification History: |
| 10 | +$Log$ |
| 11 | +Revision 1.1 2004/07/10 05:54:54 astraw |
| 12 | +First version of dynamic_image_wxagg, modified from dynamic_demo_wx by Jeremy |
| 13 | +O'Donoghue. |
| 14 | +
|
| 15 | +Revision 1.4 2004/05/03 12:12:26 jdh2358 |
| 16 | +added bang header to examples |
| 17 | +
|
| 18 | +Revision 1.3 2004/03/08 22:17:20 jdh2358 |
| 19 | +
|
| 20 | +* Fixed embedding_in_wx and dynamic_demo_wx examples |
| 21 | +
|
| 22 | +* Ported build to darwin |
| 23 | +
|
| 24 | +* Tk: |
| 25 | +
|
| 26 | + removed default figman=None from nav toolbar since it needs the |
| 27 | + figman |
| 28 | +
|
| 29 | + fixed close bug |
| 30 | +
|
| 31 | + small changes to aid darwin build |
| 32 | +
|
| 33 | +Revision 1.2 2004/02/26 20:22:58 jaytmiller |
| 34 | +Added the "numerix" Numeric/numarray selector module enabling matplotlib |
| 35 | +to work with either numarray or Numeric. See matplotlib.numerix.__doc__. |
| 36 | +
|
| 37 | +Revision 1.1 2003/12/30 17:22:09 jodonoghue |
| 38 | +First version of dynamic_demo for backend_wx |
| 39 | +""" |
| 40 | + |
| 41 | +import matplotlib |
| 42 | +from matplotlib.matlab import cm |
| 43 | +matplotlib.use('WXAgg') |
| 44 | +from matplotlib.backends.backend_wxagg import Toolbar, FigureCanvasWxAgg,\ |
| 45 | + FigureManager |
| 46 | + |
| 47 | +from matplotlib.figure import Figure |
| 48 | +from matplotlib.axes import Subplot |
| 49 | +import numarray # segfaults w/ Numeric 23.1 ADS |
| 50 | +from wxPython.wx import * |
| 51 | + |
| 52 | + |
| 53 | +TIMER_ID = wxNewId() |
| 54 | + |
| 55 | +class PlotFigure(wxFrame): |
| 56 | + |
| 57 | + def __init__(self): |
| 58 | + wxFrame.__init__(self, None, -1, "Test embedded wxFigure") |
| 59 | + |
| 60 | + self.fig = Figure((5,4), 75) |
| 61 | + self.canvas = FigureCanvasWxAgg(self, -1, self.fig) |
| 62 | + self.toolbar = Toolbar(self.canvas) |
| 63 | + self.toolbar.Realize() |
| 64 | + |
| 65 | + # On Windows, default frame size behaviour is incorrect |
| 66 | + # you don't need this under Linux |
| 67 | + tw, th = self.toolbar.GetSizeTuple() |
| 68 | + fw, fh = self.canvas.GetSizeTuple() |
| 69 | + self.toolbar.SetSize(wxSize(fw, th)) |
| 70 | + |
| 71 | + # Create a figure manager to manage things |
| 72 | + self.figmgr = FigureManager(self.canvas, 1, self) |
| 73 | + # Now put all into a sizer |
| 74 | + sizer = wxBoxSizer(wxVERTICAL) |
| 75 | + # This way of adding to sizer allows resizing |
| 76 | + sizer.Add(self.canvas, 1, wxLEFT|wxTOP|wxGROW) |
| 77 | + # Best to allow the toolbar to resize! |
| 78 | + sizer.Add(self.toolbar, 0, wxGROW) |
| 79 | + self.SetSizer(sizer) |
| 80 | + self.Fit() |
| 81 | + EVT_TIMER(self, TIMER_ID, self.onTimer) |
| 82 | +# EVT_ERASE_BACKGROUND( self, self.onEraseBackground) |
| 83 | + |
| 84 | + def init_plot_data(self): |
| 85 | + a = self.figmgr.add_subplot(111) |
| 86 | + self.x = numarray.arange(120.0)*2*numarray.pi/120.0 |
| 87 | + self.x.resize((100,120)) |
| 88 | + self.y = numarray.arange(100.0)*2*numarray.pi/100.0 |
| 89 | + self.y.resize((120,100)) |
| 90 | + self.y = numarray.transpose(self.y) |
| 91 | + z = numarray.sin(self.x) + numarray.cos(self.y) |
| 92 | + self.im = a.imshow( z, cmap=cm.jet)#, interpolation='nearest') |
| 93 | + |
| 94 | + def GetToolBar(self): |
| 95 | + # You will need to override GetToolBar if you are using an |
| 96 | + # unmanaged toolbar in your frame |
| 97 | + return self.toolbar |
| 98 | + |
| 99 | + def onTimer(self, evt): |
| 100 | + self.x += numarray.pi/15 |
| 101 | + self.y += numarray.pi/20 |
| 102 | + z = numarray.sin(self.x) + numarray.cos(self.y) |
| 103 | + self.im.set_array(z) |
| 104 | + self.canvas.draw() |
| 105 | + self.canvas.gui_repaint() |
| 106 | + |
| 107 | + def onEraseBackground(self, evt): |
| 108 | + # this is supposed to prevent redraw flicker on some X servers |
| 109 | + pass |
| 110 | + |
| 111 | +if __name__ == '__main__': |
| 112 | + app = wxPySimpleApp() |
| 113 | + frame = PlotFigure() |
| 114 | + frame.init_plot_data() |
| 115 | + |
| 116 | + # Initialise the timer - wxPython requires this to be connected to |
| 117 | + # the receiving event handler |
| 118 | + t = wxTimer(frame, TIMER_ID) |
| 119 | + t.Start(200) |
| 120 | + |
| 121 | + frame.Show() |
| 122 | + app.MainLoop() |
0 commit comments