11#!/usr/bin/env python
22"""
3- An example of how to use wx or wxagg in an application w/o the toolbar
3+ An example of how to use wx or wxagg in an application w. or w /o the toolbar
44"""
55
66from matplotlib .numerix import arange , sin , pi
1414# comment out the following to use wx rather than wxagg
1515matplotlib .use ('WXAgg' )
1616from matplotlib .backends .backend_wxagg import FigureCanvasWxAgg as FigureCanvas
17+ from matplotlib .backends .backend_wx import NavigationToolbarWx as NavigationToolbar
1718
1819from matplotlib .figure import Figure
1920
@@ -27,7 +28,7 @@ def __init__(self):
2728
2829 self .SetBackgroundColour (wxNamedColor ("WHITE" ))
2930
30- self .figure = Figure (figsize = ( 5 , 4 ), dpi = 100 )
31+ self .figure = Figure ()
3132 self .axes = self .figure .add_subplot (111 )
3233 t = arange (0.0 ,3.0 ,0.01 )
3334 s = sin (2 * pi * t )
@@ -38,9 +39,35 @@ def __init__(self):
3839
3940 self .sizer = wxBoxSizer (wxVERTICAL )
4041 self .sizer .Add (self .canvas , 1 , wxTOP | wxLEFT | wxEXPAND )
42+
43+ #self.add_toolbar() # comment this out for no toolbar
44+
45+
4146 # Capture the paint message
4247 EVT_PAINT (self , self .OnPaint )
4348
49+ def add_toolbar (self ):
50+ self .toolbar = NavigationToolbar (self .canvas , True )
51+ self .toolbar .Realize ()
52+ if wxPlatform == '__WXMAC__' :
53+ # Mac platform (OSX 10.3, MacPython) does not seem to cope with
54+ # having a toolbar in a sizer. This work-around gets the buttons
55+ # back, but at the expense of having the toolbar at the top
56+ self .SetToolBar (self .toolbar )
57+ else :
58+ # On Windows platform, default window size is incorrect, so set
59+ # toolbar width to figure width.
60+ tw , th = self .toolbar .GetSizeTuple ()
61+ fw , fh = self .canvas .GetSizeTuple ()
62+ # By adding toolbar in sizer, we are able to put it at the bottom
63+ # of the frame - so appearance is closer to GTK version.
64+ # As noted above, doesn't work for Mac.
65+ self .toolbar .SetSize (wxSize (fw , th ))
66+ self .sizer .Add (self .toolbar , 0 , wxLEFT | wxEXPAND )
67+ # update the axes menu on the toolbar
68+ self .toolbar .update ()
69+
70+
4471 def OnPaint (self , event ):
4572 self .canvas .draw ()
4673
0 commit comments