@@ -399,24 +399,75 @@ 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
+
437
+ class ToolGrid (_ToolGridBase ):
438
+ """Tool to toggle the major grids of the figure"""
439
+
440
+ description = 'Toogle major grids'
441
+ default_keymap = rcParams ['keymap.grid' ]
442
+
443
+ def _get_next_grid_states (self , ax ):
444
+ x_state , y_state = map (self ._get_uniform_grid_state ,
445
+ [ax .xaxis .majorTicks , ax .yaxis .majorTicks ])
446
+ cycle = self ._cycle
447
+ # Bail out if major grids are not in a uniform state.
448
+ x_state , y_state = (
449
+ cycle [(cycle .index ((x_state , y_state )) + 1 ) % len (cycle )])
450
+ return x_state , y_state , "major"
451
+
452
+
453
+ class ToolMinorGrid (_ToolGridBase ):
454
+ """Tool to toggle the major and minor grids of the figure"""
455
+
456
+ description = 'Toogle major and minor grids'
457
+ default_keymap = rcParams ['keymap.grid_minor' ]
458
+
459
+ def _get_next_grid_states (self , ax ):
460
+ if None in map (self ._get_uniform_grid_state ,
461
+ [ax .xaxis .majorTicks , ax .yaxis .majorTicks ]):
462
+ # Bail out if major grids are not in a uniform state.
463
+ raise ValueError
464
+ x_state , y_state = map (self ._get_uniform_grid_state ,
465
+ [ax .xaxis .minorTicks , ax .yaxis .minorTicks ])
466
+ cycle = self ._cycle
467
+ # Bail out if minor 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 , "both"
420
471
421
472
422
473
class ToolFullScreen (ToolToggleBase ):
@@ -944,6 +995,7 @@ def _mouse_move(self, event):
944
995
'subplots' : 'ToolConfigureSubplots' ,
945
996
'save' : 'ToolSaveFigure' ,
946
997
'grid' : ToolGrid ,
998
+ 'grid_minor' : ToolMinorGrid ,
947
999
'fullscreen' : ToolFullScreen ,
948
1000
'quit' : ToolQuit ,
949
1001
'quit_all' : ToolQuitAll ,
0 commit comments