@@ -2485,7 +2485,7 @@ def key_press_handler(event, canvas, toolbar=None):
2485
2485
grid_minor_keys = rcParams ['keymap.grid_minor' ]
2486
2486
toggle_yscale_keys = rcParams ['keymap.yscale' ]
2487
2487
toggle_xscale_keys = rcParams ['keymap.xscale' ]
2488
- all = rcParams ['keymap.all_axes' ]
2488
+ all_keys = rcParams ['keymap.all_axes' ]
2489
2489
2490
2490
# toggle fullscreen mode (default key 'f')
2491
2491
if event .key in fullscreen_keys :
@@ -2525,27 +2525,52 @@ def key_press_handler(event, canvas, toolbar=None):
2525
2525
return
2526
2526
2527
2527
# these bindings require the mouse to be over an axes to trigger
2528
+ def _get_uniform_gridstate (ticks ):
2529
+ # Return True/False if all grid lines are on or off, None if they are
2530
+ # not all in the same state.
2531
+ if all (tick .gridOn for tick in ticks ):
2532
+ return True
2533
+ elif not any (tick .gridOn for tick in ticks ):
2534
+ return False
2535
+ else :
2536
+ return None
2528
2537
2529
2538
ax = event .inaxes
2530
- # switching on/off a grid in current axes (default key 'g')
2539
+ # toggle major grids in current axes (default key 'g')
2540
+ # Both here and below (for 'G'), we do nothing is the grids are not in a
2541
+ # uniform state, to avoid messing up user customization.
2531
2542
if event .key in grid_keys :
2532
- # If either major grid is on, turn all major and minor grids off.
2533
- if any (tick .gridOn
2534
- for tick in ax .xaxis .majorTicks + ax .yaxis .majorTicks ):
2535
- ax .grid (False , which = "both" )
2536
- # Otherwise, turn the major grids on.
2543
+ x_state = _get_uniform_gridstate (ax .xaxis .majorTicks )
2544
+ y_state = _get_uniform_gridstate (ax .yaxis .majorTicks )
2545
+ cycle = [(False , False ), (True , False ), (True , True ), (False , True )]
2546
+ try :
2547
+ x_state , y_state = (
2548
+ cycle [(cycle .index ((x_state , y_state )) + 1 ) % len (cycle )])
2549
+ except ValueError :
2550
+ # Exclude major grids not in a uniform state.
2551
+ pass
2537
2552
else :
2538
- ax .grid (True )
2539
- canvas .draw ()
2540
- if event .key in grid_minor_keys :
2541
- # If either minor grid is on, turn all minor grids off.
2542
- if any (tick .gridOn
2543
- for tick in ax .xaxis .minorTicks + ax .yaxis .minorTicks ):
2544
- ax .grid (False , which = "minor" )
2545
- # Otherwise, turn all major and minor grids on.
2553
+ ax .grid (x_state , which = "major" , axis = "x" )
2554
+ ax .grid (y_state , which = "major" , axis = "y" )
2555
+ canvas .draw ()
2556
+ # toggle major and minor grids in current axes (default key 'G')
2557
+ if (event .key in grid_minor_keys
2558
+ # Exclude major grids not in a uniform state.
2559
+ and None not in [_get_uniform_gridstate (ax .xaxis .majorTicks ),
2560
+ _get_uniform_gridstate (ax .yaxis .majorTicks )]):
2561
+ x_state = _get_uniform_gridstate (ax .xaxis .minorTicks )
2562
+ y_state = _get_uniform_gridstate (ax .yaxis .minorTicks )
2563
+ cycle = [(False , False ), (True , False ), (True , True ), (False , True )]
2564
+ try :
2565
+ x_state , y_state = (
2566
+ cycle [(cycle .index ((x_state , y_state )) + 1 ) % len (cycle )])
2567
+ except ValueError :
2568
+ # Exclude minor grids not in a uniform state.
2569
+ pass
2546
2570
else :
2547
- ax .grid (True , which = "both" )
2548
- canvas .draw ()
2571
+ ax .grid (x_state , which = "both" , axis = "x" )
2572
+ ax .grid (y_state , which = "both" , axis = "y" )
2573
+ canvas .draw ()
2549
2574
# toggle scaling of y-axes between 'log and 'linear' (default key 'l')
2550
2575
elif event .key in toggle_yscale_keys :
2551
2576
scale = ax .get_yscale ()
@@ -2565,7 +2590,7 @@ def key_press_handler(event, canvas, toolbar=None):
2565
2590
ax .set_xscale ('log' )
2566
2591
ax .figure .canvas .draw ()
2567
2592
2568
- elif (event .key .isdigit () and event .key != '0' ) or event .key in all :
2593
+ elif (event .key .isdigit () and event .key != '0' ) or event .key in all_keys :
2569
2594
# keys in list 'all' enables all axes (default key 'a'),
2570
2595
# otherwise if key is a number only enable this particular axes
2571
2596
# if it was the axes, where the event was raised
0 commit comments