Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d7d940f

Browse files
committed
Cycle the grid state (none->x->xy->y) for 'g'/'G'.
1 parent e8de04c commit d7d940f

File tree

1 file changed

+43
-18
lines changed

1 file changed

+43
-18
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2485,7 +2485,7 @@ def key_press_handler(event, canvas, toolbar=None):
24852485
grid_minor_keys = rcParams['keymap.grid_minor']
24862486
toggle_yscale_keys = rcParams['keymap.yscale']
24872487
toggle_xscale_keys = rcParams['keymap.xscale']
2488-
all = rcParams['keymap.all_axes']
2488+
all_keys = rcParams['keymap.all_axes']
24892489

24902490
# toggle fullscreen mode (default key 'f')
24912491
if event.key in fullscreen_keys:
@@ -2525,27 +2525,52 @@ def key_press_handler(event, canvas, toolbar=None):
25252525
return
25262526

25272527
# 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
25282537

25292538
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.
25312542
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
25372552
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
25462570
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()
25492574
# toggle scaling of y-axes between 'log and 'linear' (default key 'l')
25502575
elif event.key in toggle_yscale_keys:
25512576
scale = ax.get_yscale()
@@ -2565,7 +2590,7 @@ def key_press_handler(event, canvas, toolbar=None):
25652590
ax.set_xscale('log')
25662591
ax.figure.canvas.draw()
25672592

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:
25692594
# keys in list 'all' enables all axes (default key 'a'),
25702595
# otherwise if key is a number only enable this particular axes
25712596
# if it was the axes, where the event was raised

0 commit comments

Comments
 (0)