11#!/usr/bin/env python
22"""
3- Copyright (C) 2003-2004 Jeremy O'Donoghue, Andrew Straw and others
3+ Copyright (C) 2003-2004 Jeremy O'Donoghue and others
44
55License: This work is licensed under the PSF. A copy should be included
66with this source code, and is also available at
77http://www.python.org/psf/license.html
88
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:
9+ """
10+ import sys , time , os , gc
2511
26- removed default figman=None from nav toolbar since it needs the
27- figman
12+ import matplotlib
13+ matplotlib . use ( 'WXAgg' )
2814
29- fixed close bug
15+ # jdh: you need to control Numeric vs numarray with numerix, otherwise
16+ # matplotlib may be using numeric under the hood and while you are
17+ # using numarray and this isn't efficient. Also, if you use
18+ # numerix=numarray, it is important to compile matplotlib for numarray
19+ # by setting NUMERIX = 'numarray' in setup.py before building
20+ from matplotlib import rcParams
21+ rcParams ['numerix' ] = 'numarray'
3022
31- small changes to aid darwin build
3223
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__.
24+ # jdh: you can import cm directly, you don't need to go via
25+ # matplotlib.matlab
26+ import matplotlib . cm as cm
3627
37- Revision 1.1 2003/12/30 17:22:09 jodonoghue
38- First version of dynamic_demo for backend_wx
39- """
28+ from matplotlib .backends .backend_wxagg import Toolbar , FigureCanvasWxAgg
4029
41- import matplotlib
42- from matplotlib .matlab import cm
43- matplotlib .use ('WXAgg' )
44- from matplotlib .backends .backend_wxagg import Toolbar , FigureCanvasWxAgg ,\
45- FigureManager
30+ # jdh: you don't need a figure manager in the GUI - this class was
31+ # designed for the matlab interface
4632
4733from matplotlib .figure import Figure
48- from matplotlib .axes import Subplot
49- import numarray # segfaults w/ Numeric 23.1 ADS
34+ import matplotlib .numerix as numerix
5035from wxPython .wx import *
5136
5237
5338TIMER_ID = wxNewId ()
5439
40+ # jdh: use this function, or something similar, when reporting a
41+ # memory leak
42+ def report_memory (i ):
43+ pid = os .getpid ()
44+ a2 = os .popen ('ps -p %d -o rss,sz' % pid ).readlines ()
45+ print i , ' ' , a2 [1 ],
46+ return int (a2 [1 ].split ()[0 ])
47+
5548class PlotFigure (wxFrame ):
5649
5750 def __init__ (self ):
@@ -69,7 +62,7 @@ def __init__(self):
6962 self .toolbar .SetSize (wxSize (fw , th ))
7063
7164 # Create a figure manager to manage things
72- self . figmgr = FigureManager ( self . canvas , 1 , self )
65+
7366 # Now put all into a sizer
7467 sizer = wxBoxSizer (wxVERTICAL )
7568 # This way of adding to sizer allows resizing
@@ -79,33 +72,50 @@ def __init__(self):
7972 self .SetSizer (sizer )
8073 self .Fit ()
8174 EVT_TIMER (self , TIMER_ID , self .onTimer )
82- # EVT_ERASE_BACKGROUND( self, self.onEraseBackground)
83-
75+ self . cnt = 0
76+
8477 def init_plot_data (self ):
85- a = self .figmgr .add_subplot (111 )
86- self .x = numarray .arange (120.0 )* 2 * numarray .pi / 120.0
78+ # jdh you can add a subplot directly from the fig rather than
79+ # the fig manager
80+ a = self .fig .add_subplot (111 )
81+ self .x = numerix .arange (120.0 )* 2 * numerix .pi / 120.0
8782 self .x .resize ((100 ,120 ))
88- self .y = numarray .arange (100.0 )* 2 * numarray .pi / 100.0
83+ self .y = numerix .arange (100.0 )* 2 * numerix .pi / 100.0
8984 self .y .resize ((120 ,100 ))
90- self .y = numarray .transpose (self .y )
91- z = numarray .sin (self .x ) + numarray .cos (self .y )
85+ self .y = numerix .transpose (self .y )
86+ z = numerix .sin (self .x ) + numerix .cos (self .y )
9287 self .im = a .imshow ( z , cmap = cm .jet )#, interpolation='nearest')
9388
89+
90+
9491 def GetToolBar (self ):
95- # You will need to override GetToolBar if you are using an
92+ # You will need to override GetToolBar if you are using an
9693 # unmanaged toolbar in your frame
9794 return self .toolbar
9895
9996 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 )
97+ self .x += numerix .pi / 15
98+ self .y += numerix .pi / 20
99+ z = numerix .sin (self .x ) + numerix .cos (self .y )
103100 self .im .set_array (z )
104101 self .canvas .draw ()
105- self .canvas .gui_repaint ()
102+ #self.canvas.gui_repaint() # jdh wxagg_draw calls this already
103+
104+ val = report_memory (self .cnt )
105+ if self .cnt == 1 :
106+ self .start = val # skip cnt=0
107+ self .tstart = time .time ()
108+ elif self .cnt == 50 :
109+ end = val
110+ print 'Average memory consumed per loop: %1.4f\n ' % ((end - self .start )/ float (self .cnt ))
111+ print 'FPS' , self .cnt / (time .time () - self .tstart )
112+ sys .exit ()
113+
114+ self .cnt += 1
115+ gc .collect ()
106116
107117 def onEraseBackground (self , evt ):
108- # this is supposed to prevent redraw flicker on some X servers
118+ # this is supposed to prevent redraw flicker on some X servers...
109119 pass
110120
111121if __name__ == '__main__' :
@@ -120,3 +130,4 @@ def onEraseBackground(self, evt):
120130
121131 frame .Show ()
122132 app .MainLoop ()
133+
0 commit comments