1515from matplotlib ._pylab_helpers import Gcf
1616from matplotlib .figure import Figure
1717from matplotlib .mathtext import math_parse_s_ft2font
18+ from matplotlib .widgets import SubplotTool
19+
1820import qt
1921
2022backend_version = "0.9.1"
@@ -164,22 +166,16 @@ def __init__( self, canvas, num ):
164166 self .window .setCaption ( "Figure %d" % num )
165167
166168 self .window ._destroying = False
167-
168- if matplotlib .rcParams ['toolbar' ] == 'classic' :
169- print "Classic toolbar is not yet supported"
170- #self.toolbar = NavigationToolbarQT( centralWidget, canvas )
171- self .toolbar = None
172- elif matplotlib .rcParams ['toolbar' ] == 'toolbar2' :
173- self .toolbar = NavigationToolbar2QT ( centralWidget , canvas )
174- else :
175- self .toolbar = None
169+
170+ self .toolbar = self ._get_toolbar (self .canvas , centralWidget )
176171
177172 # Use a vertical layout for the plot and the toolbar. Set the
178173 # stretch to all be in the plot so the toolbar doesn't resize.
179- layout = qt .QVBoxLayout ( centralWidget )
180- layout .addWidget ( self .canvas , 1 )
174+ self .layout = qt .QVBoxLayout ( centralWidget )
175+ self .layout .addWidget ( self .canvas , 1 )
176+
181177 if self .toolbar :
182- layout .addWidget ( self .toolbar , 0 )
178+ self . layout .addWidget ( self .toolbar , 0 )
183179
184180 self .window .setCentralWidget ( centralWidget )
185181
@@ -212,6 +208,21 @@ def _widgetclosed( self ):
212208 def _widgetCloseEvent ( self , event ):
213209 self ._widgetclosed ()
214210 qt .QWidget .closeEvent ( self .window , event )
211+
212+ def _get_toolbar (self , canvas , parent ):
213+ # must be inited after the window, drawingArea and figure
214+ # attrs are set
215+ if matplotlib .rcParams ['toolbar' ] == 'classic' :
216+ print "Classic toolbar is not yet supported"
217+ elif matplotlib .rcParams ['toolbar' ] == 'toolbar2' :
218+ toolbar = NavigationToolbar2QT (canvas , parent )
219+ else :
220+ toolbar = None
221+ return toolbar
222+
223+ def set_canvas_size (self , width , height ):
224+ 'set the canvas size in pixels'
225+ self .window .resize (width , height )
215226
216227 def destroy ( self , * args ):
217228 if self .window ._destroying : return
@@ -230,10 +241,11 @@ class NavigationToolbar2QT( NavigationToolbar2, qt.QWidget ):
230241 ('Pan' , 'Pan axes with left mouse, zoom with right' , 'move.ppm' , 'pan' ),
231242 ('Zoom' , 'Zoom to rectangle' ,'zoom_to_rect.ppm' , 'zoom' ),
232243 (None , None , None , None ),
244+ ('Subplots' , 'Configure subplots' ,'subplots.png' , 'configure_subplots' ),
233245 ('Save' , 'Save the figure' ,'filesave.ppm' , 'save_figure' ),
234246 )
235247
236- def __init__ ( self , parent , canvas ):
248+ def __init__ ( self , canvas , parent ):
237249 self .canvas = canvas
238250 self .buttons = {}
239251
@@ -282,6 +294,9 @@ def _init_toolbar( self ):
282294 self .locLabel .setSizePolicy (qt .QSizePolicy (qt .QSizePolicy .Ignored ,
283295 qt .QSizePolicy .Ignored ))
284296 self .layout .addWidget ( self .locLabel , 1 )
297+
298+ # reference holder for subplots_adjust window
299+ self .adj_window = None
285300
286301 def pan ( self , * args ):
287302 self .buttons [ 'Zoom' ].setOn ( False )
@@ -312,6 +327,32 @@ def draw_rubberband( self, event, x0, y0, x1, y1 ):
312327
313328 rect = [ int (val )for val in min (x0 ,x1 ), min (y0 , y1 ), w , h ]
314329 self .canvas .drawRectangle ( rect )
330+
331+ def configure_subplots (self ):
332+ self .adj_window = qt .QMainWindow ()
333+ win = self .adj_window
334+ win .setCaption ("Subplot Configuration Tool" )
335+
336+ toolfig = Figure (figsize = (6 ,3 ))
337+ toolfig .subplots_adjust (top = 0.9 )
338+ w = int (toolfig .bbox .width ())
339+ h = int (toolfig .bbox .height ())
340+
341+ canvas = self ._get_canvas (toolfig )
342+ tool = SubplotTool (self .canvas .figure , toolfig )
343+ centralWidget = qt .QWidget (win )
344+ canvas .reparent (centralWidget , qt .QPoint (0 , 0 ))
345+ win .setCentralWidget (centralWidget )
346+
347+ layout = qt .QVBoxLayout (centralWidget )
348+ layout .addWidget (canvas , 1 )
349+
350+ win .resize (w , h )
351+ canvas .setFocus ()
352+ win .show ()
353+
354+ def _get_canvas (self , fig ):
355+ return FigureCanvasQT (fig )
315356
316357 def save_figure ( self ):
317358 fname = qt .QFileDialog .getSaveFileName ()
0 commit comments