@@ -399,24 +399,74 @@ def trigger(self, sender, event, data=None):
399
399
a .set_navigate (i == n )
400
400
401
401
402
- class ToolGrid (ToolToggleBase ):
403
- """Tool to toggle the grid of the figure"""
402
+ class _ToolGridBase (ToolBase ):
403
+ """Common functionality between ToolGrid and ToolMinorGrid.
404
+ """
404
405
405
- description = 'Toogle Grid'
406
- default_keymap = rcParams ['keymap.grid' ]
406
+ _cycle = [(False , False ), (True , False ), (True , True ), (False , True )]
407
407
408
408
def trigger (self , sender , event , data = None ):
409
- if event .inaxes is None :
409
+ ax = event .inaxes
410
+ if ax is None :
410
411
return
411
- ToolToggleBase .trigger (self , sender , event , data )
412
+ try :
413
+ x_state , y_state , which = self ._get_next_grid_states (ax )
414
+ except ValueError :
415
+ pass
416
+ else :
417
+ ax .grid (x_state , which = which , axis = "x" )
418
+ ax .grid (y_state , which = which , axis = "y" )
419
+ ax .figure .canvas .draw_idle ()
412
420
413
- def enable ( self , event ):
414
- event . inaxes . grid ( True )
415
- self . figure . canvas . draw_idle ()
421
+ @ staticmethod
422
+ def _get_uniform_grid_state ( ticks ):
423
+ """Check whether all grid lines are in the same visibility state.
416
424
417
- def disable (self , event ):
418
- event .inaxes .grid (False )
419
- self .figure .canvas .draw_idle ()
425
+ Returns True/False if all grid lines are on or off, None if they are
426
+ not all in the same state.
427
+ """
428
+ if all (tick .gridOn for tick in ticks ):
429
+ return True
430
+ elif not any (tick .gridOn for tick in ticks ):
431
+ return False
432
+ else :
433
+ return None
434
+
435
+
436
+ class ToolGrid (_ToolGridBase ):
437
+ """Tool to toggle the major grids of the figure"""
438
+
439
+ description = 'Toogle major grids'
440
+ default_keymap = rcParams ['keymap.grid' ]
441
+
442
+ def _get_next_grid_states (self , ax ):
443
+ x_state , y_state = map (self ._get_uniform_grid_state ,
444
+ [ax .xaxis .majorTicks , ax .yaxis .majorTicks ])
445
+ cycle = self ._cycle
446
+ # Bail out if major grids are not in a uniform state.
447
+ x_state , y_state = (
448
+ cycle [(cycle .index ((x_state , y_state )) + 1 ) % len (cycle )])
449
+ return x_state , y_state , "major"
450
+
451
+
452
+ class ToolMinorGrid (_ToolGridBase ):
453
+ """Tool to toggle the major and minor grids of the figure"""
454
+
455
+ description = 'Toogle major and minor grids'
456
+ default_keymap = rcParams ['keymap.grid_minor' ]
457
+
458
+ def _get_next_grid_states (self , ax ):
459
+ if None in map (self ._get_uniform_grid_state ,
460
+ [ax .xaxis .majorTicks , ax .yaxis .majorTicks ]):
461
+ # Bail out if major grids are not in a uniform state.
462
+ raise ValueError
463
+ x_state , y_state = map (self ._get_uniform_grid_state ,
464
+ [ax .xaxis .minorTicks , ax .yaxis .minorTicks ])
465
+ cycle = self ._cycle
466
+ # Bail out if minor grids are not in a uniform state.
467
+ x_state , y_state = (
468
+ cycle [(cycle .index ((x_state , y_state )) + 1 ) % len (cycle )])
469
+ return x_state , y_state , "both"
420
470
421
471
422
472
class ToolFullScreen (ToolToggleBase ):
@@ -944,6 +994,7 @@ def _mouse_move(self, event):
944
994
'subplots' : 'ToolConfigureSubplots' ,
945
995
'save' : 'ToolSaveFigure' ,
946
996
'grid' : ToolGrid ,
997
+ 'grid_minor' : ToolMinorGrid ,
947
998
'fullscreen' : ToolFullScreen ,
948
999
'quit' : ToolQuit ,
949
1000
'quit_all' : ToolQuitAll ,
0 commit comments