@@ -420,24 +420,74 @@ def trigger(self, sender, event, data=None):
420420 a .set_navigate (i == n )
421421
422422
423- class ToolGrid ( ToolToggleBase ):
424- """Tool to toggle the grid of the figure """
423+ class _ToolGridBase ( ToolBase ):
424+ """Common functionality between ToolGrid and ToolMinorGrid. """
425425
426- description = 'Toogle Grid'
427- default_keymap = rcParams ['keymap.grid' ]
426+ _cycle = [(False , False ), (True , False ), (True , True ), (False , True )]
428427
429428 def trigger (self , sender , event , data = None ):
430- if event .inaxes is None :
429+ ax = event .inaxes
430+ if ax is None :
431431 return
432- ToolToggleBase .trigger (self , sender , event , data )
432+ try :
433+ x_state , y_state , which = self ._get_next_grid_states (ax )
434+ except ValueError :
435+ pass
436+ else :
437+ ax .grid (x_state , which = which , axis = "x" )
438+ ax .grid (y_state , which = which , axis = "y" )
439+ ax .figure .canvas .draw_idle ()
433440
434- def enable (self , event ):
435- event .inaxes .grid (True )
436- self .figure .canvas .draw_idle ()
441+ @staticmethod
442+ def _get_uniform_grid_state (ticks ):
443+ """
444+ Check whether all grid lines are in the same visibility state.
437445
438- def disable (self , event ):
439- event .inaxes .grid (False )
440- self .figure .canvas .draw_idle ()
446+ Returns True/False if all grid lines are on or off, None if they are
447+ not all in the same state.
448+ """
449+ if all (tick .gridOn for tick in ticks ):
450+ return True
451+ elif not any (tick .gridOn for tick in ticks ):
452+ return False
453+ else :
454+ return None
455+
456+
457+ class ToolGrid (_ToolGridBase ):
458+ """Tool to toggle the major grids of the figure"""
459+
460+ description = 'Toogle major grids'
461+ default_keymap = rcParams ['keymap.grid' ]
462+
463+ def _get_next_grid_states (self , ax ):
464+ x_state , y_state = map (self ._get_uniform_grid_state ,
465+ [ax .xaxis .majorTicks , ax .yaxis .majorTicks ])
466+ cycle = self ._cycle
467+ # Bail out (via ValueError) if major grids are not in a uniform state.
468+ x_state , y_state = (
469+ cycle [(cycle .index ((x_state , y_state )) + 1 ) % len (cycle )])
470+ return x_state , y_state , "major"
471+
472+
473+ class ToolMinorGrid (_ToolGridBase ):
474+ """Tool to toggle the major and minor grids of the figure"""
475+
476+ description = 'Toogle major and minor grids'
477+ default_keymap = rcParams ['keymap.grid_minor' ]
478+
479+ def _get_next_grid_states (self , ax ):
480+ if None in map (self ._get_uniform_grid_state ,
481+ [ax .xaxis .majorTicks , ax .yaxis .majorTicks ]):
482+ # Bail out if major grids are not in a uniform state.
483+ raise ValueError
484+ x_state , y_state = map (self ._get_uniform_grid_state ,
485+ [ax .xaxis .minorTicks , ax .yaxis .minorTicks ])
486+ cycle = self ._cycle
487+ # Bail out (via ValueError) if minor grids are not in a uniform state.
488+ x_state , y_state = (
489+ cycle [(cycle .index ((x_state , y_state )) + 1 ) % len (cycle )])
490+ return x_state , y_state , "both"
441491
442492
443493class ToolFullScreen (ToolToggleBase ):
@@ -965,6 +1015,7 @@ def _mouse_move(self, event):
9651015 'subplots' : 'ToolConfigureSubplots' ,
9661016 'save' : 'ToolSaveFigure' ,
9671017 'grid' : ToolGrid ,
1018+ 'grid_minor' : ToolMinorGrid ,
9681019 'fullscreen' : ToolFullScreen ,
9691020 'quit' : ToolQuit ,
9701021 'quit_all' : ToolQuitAll ,
0 commit comments