@@ -1436,7 +1436,7 @@ def __init__(self, figure):
14361436 self .button_pick_id = self .mpl_connect ('button_press_event' ,self .pick )
14371437 self .scroll_pick_id = self .mpl_connect ('scroll_event' ,self .pick )
14381438 self .mouse_grabber = None # the axes currently grabbing mouse
1439-
1439+ self . toolbar = None # NavigationToolbar2 will set me
14401440 if False :
14411441 ## highlight the artists that are hit
14421442 self .mpl_connect ('motion_notify_event' ,self .onHilite )
@@ -2213,6 +2213,110 @@ def stop_event_loop_default(self):
22132213 self ._looping = False
22142214
22152215
2216+ def key_press_handler (event , canvas , toolbar = None ):
2217+ """
2218+ Implement the default mpl key bindings for the canvas and toolbar
2219+ described at :ref:`navigation-keyboard-shortcuts`
2220+
2221+ *event*
2222+ a :class:`KeyEvent` instance
2223+ *canvas*
2224+ a :class:`FigureCanvasBase` instance
2225+ *toolbar*
2226+ a class:`NavigationToolbar2` instance
2227+
2228+ """
2229+ # these bindings happen whether you are over an axes or not
2230+ #if event.key == 'q':
2231+ # self.destroy() # how cruel to have to destroy oneself!
2232+ # return
2233+
2234+ if event .key is None :
2235+ return
2236+
2237+ # Load key-mappings from your matplotlibrc file.
2238+ fullscreen_keys = rcParams ['keymap.fullscreen' ]
2239+ home_keys = rcParams ['keymap.home' ]
2240+ back_keys = rcParams ['keymap.back' ]
2241+ forward_keys = rcParams ['keymap.forward' ]
2242+ pan_keys = rcParams ['keymap.pan' ]
2243+ zoom_keys = rcParams ['keymap.zoom' ]
2244+ save_keys = rcParams ['keymap.save' ]
2245+ grid_keys = rcParams ['keymap.grid' ]
2246+ toggle_yscale_keys = rcParams ['keymap.yscale' ]
2247+ toggle_xscale_keys = rcParams ['keymap.xscale' ]
2248+ all = rcParams ['keymap.all_axes' ]
2249+
2250+ # toggle fullscreen mode (default key 'f')
2251+ if event .key in fullscreen_keys :
2252+ self .full_screen_toggle ()
2253+
2254+ if toolbar is not None :
2255+ # home or reset mnemonic (default key 'h', 'home' and 'r')
2256+ if event .key in home_keys :
2257+ toolbar .home ()
2258+ # forward / backward keys to enable left handed quick navigation
2259+ # (default key for backward: 'left', 'backspace' and 'c')
2260+ elif event .key in back_keys :
2261+ toolbar .back ()
2262+ # (default key for forward: 'right' and 'v')
2263+ elif event .key in forward_keys :
2264+ toolbar .forward ()
2265+ # pan mnemonic (default key 'p')
2266+ elif event .key in pan_keys :
2267+ toolbar .pan ()
2268+ # zoom mnemonic (default key 'o')
2269+ elif event .key in zoom_keys :
2270+ toolbar .zoom ()
2271+ # saving current figure (default key 's')
2272+ elif event .key in save_keys :
2273+ toolbar .save_figure ()
2274+
2275+ if event .inaxes is None :
2276+ return
2277+
2278+ # the mouse has to be over an axes to trigger these
2279+ # switching on/off a grid in current axes (default key 'g')
2280+ if event .key in grid_keys :
2281+ event .inaxes .grid ()
2282+ canvas .draw ()
2283+ # toggle scaling of y-axes between 'log and 'linear' (default key 'l')
2284+ elif event .key in toggle_yscale_keys :
2285+ ax = event .inaxes
2286+ scale = ax .get_yscale ()
2287+ if scale == 'log' :
2288+ ax .set_yscale ('linear' )
2289+ ax .figure .canvas .draw ()
2290+ elif scale == 'linear' :
2291+ ax .set_yscale ('log' )
2292+ ax .figure .canvas .draw ()
2293+ # toggle scaling of x-axes between 'log and 'linear' (default key 'k')
2294+ elif event .key in toggle_xscale_keys :
2295+ ax = event .inaxes
2296+ scalex = ax .get_xscale ()
2297+ if scalex == 'log' :
2298+ ax .set_xscale ('linear' )
2299+ ax .figure .canvas .draw ()
2300+ elif scalex == 'linear' :
2301+ ax .set_xscale ('log' )
2302+ ax .figure .canvas .draw ()
2303+
2304+ elif (event .key .isdigit () and event .key != '0' ) or event .key in all :
2305+ # keys in list 'all' enables all axes (default key 'a'),
2306+ # otherwise if key is a number only enable this particular axes
2307+ # if it was the axes, where the event was raised
2308+ if not (event .key in all ):
2309+ n = int (event .key )- 1
2310+ for i , a in enumerate (canvas .figure .get_axes ()):
2311+ # consider axes, in which the event was raised
2312+ # FIXME: Why only this axes?
2313+ if event .x is not None and event .y is not None \
2314+ and a .in_axes (event ):
2315+ if event .key in all :
2316+ a .set_navigate (True )
2317+ else :
2318+ a .set_navigate (i == n )
2319+
22162320
22172321class FigureManagerBase :
22182322 """
@@ -2244,97 +2348,11 @@ def resize(self, w, h):
22442348 pass
22452349
22462350 def key_press (self , event ):
2247-
2248- # these bindings happen whether you are over an axes or not
2249- #if event.key == 'q':
2250- # self.destroy() # how cruel to have to destroy oneself!
2251- # return
2252-
2253- if event .key is None :
2254- return
2255-
2256- # Load key-mappings from your matplotlibrc file.
2257- fullscreen_keys = rcParams ['keymap.fullscreen' ]
2258- home_keys = rcParams ['keymap.home' ]
2259- back_keys = rcParams ['keymap.back' ]
2260- forward_keys = rcParams ['keymap.forward' ]
2261- pan_keys = rcParams ['keymap.pan' ]
2262- zoom_keys = rcParams ['keymap.zoom' ]
2263- save_keys = rcParams ['keymap.save' ]
2264- grid_keys = rcParams ['keymap.grid' ]
2265- toggle_yscale_keys = rcParams ['keymap.yscale' ]
2266- toggle_xscale_keys = rcParams ['keymap.xscale' ]
2267- all = rcParams ['keymap.all_axes' ]
2268-
2269- # toggle fullscreen mode (default key 'f')
2270- if event .key in fullscreen_keys :
2271- self .full_screen_toggle ()
2272-
2273- # home or reset mnemonic (default key 'h', 'home' and 'r')
2274- elif event .key in home_keys :
2275- self .canvas .toolbar .home ()
2276- # forward / backward keys to enable left handed quick navigation
2277- # (default key for backward: 'left', 'backspace' and 'c')
2278- elif event .key in back_keys :
2279- self .canvas .toolbar .back ()
2280- # (default key for forward: 'right' and 'v')
2281- elif event .key in forward_keys :
2282- self .canvas .toolbar .forward ()
2283- # pan mnemonic (default key 'p')
2284- elif event .key in pan_keys :
2285- self .canvas .toolbar .pan ()
2286- # zoom mnemonic (default key 'o')
2287- elif event .key in zoom_keys :
2288- self .canvas .toolbar .zoom ()
2289- # saving current figure (default key 's')
2290- elif event .key in save_keys :
2291- self .canvas .toolbar .save_figure ()
2292-
2293- if event .inaxes is None :
2294- return
2295-
2296- # the mouse has to be over an axes to trigger these
2297- # switching on/off a grid in current axes (default key 'g')
2298- if event .key in grid_keys :
2299- event .inaxes .grid ()
2300- self .canvas .draw ()
2301- # toggle scaling of y-axes between 'log and 'linear' (default key 'l')
2302- elif event .key in toggle_yscale_keys :
2303- ax = event .inaxes
2304- scale = ax .get_yscale ()
2305- if scale == 'log' :
2306- ax .set_yscale ('linear' )
2307- ax .figure .canvas .draw ()
2308- elif scale == 'linear' :
2309- ax .set_yscale ('log' )
2310- ax .figure .canvas .draw ()
2311- # toggle scaling of x-axes between 'log and 'linear' (default key 'k')
2312- elif event .key in toggle_xscale_keys :
2313- ax = event .inaxes
2314- scalex = ax .get_xscale ()
2315- if scalex == 'log' :
2316- ax .set_xscale ('linear' )
2317- ax .figure .canvas .draw ()
2318- elif scalex == 'linear' :
2319- ax .set_xscale ('log' )
2320- ax .figure .canvas .draw ()
2321-
2322- elif (event .key .isdigit () and event .key != '0' ) or event .key in all :
2323- # keys in list 'all' enables all axes (default key 'a'),
2324- # otherwise if key is a number only enable this particular axes
2325- # if it was the axes, where the event was raised
2326- if not (event .key in all ):
2327- n = int (event .key )- 1
2328- for i , a in enumerate (self .canvas .figure .get_axes ()):
2329- # consider axes, in which the event was raised
2330- # FIXME: Why only this axes?
2331- if event .x is not None and event .y is not None \
2332- and a .in_axes (event ):
2333- if event .key in all :
2334- a .set_navigate (True )
2335- else :
2336- a .set_navigate (i == n )
2337-
2351+ """
2352+ implement the default mpl key bindings defined at
2353+ :ref:`navigation-keyboard-shortcuts`
2354+ """
2355+ key_press_handler (event , self .canvas , self .canvas .toolbar )
23382356
23392357 def show_popup (self , msg ):
23402358 """
0 commit comments