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

Skip to content

Commit 0c3937c

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

File tree

2 files changed

+56
-22
lines changed

2 files changed

+56
-22
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Improved toggling of the axes grids
2+
-----------------------------------
3+
The `g` key binding now switches the states of the `x` and `y` grids
4+
independently (by cycling through all four on/off combinations).
5+
6+
The new `G` key binding switches the states of the minor grids.
7+
8+
Both bindings are disabled if only a subset of the grid lines (in either
9+
direction) is visible, to avoid making irreversible changes to the figure.

lib/matplotlib/backend_bases.py

Lines changed: 47 additions & 22 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,47 +2525,72 @@ 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_idle()
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_idle()
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()
25522577
if scale == 'log':
25532578
ax.set_yscale('linear')
2554-
ax.figure.canvas.draw()
2579+
ax.figure.canvas.draw_idle()
25552580
elif scale == 'linear':
25562581
ax.set_yscale('log')
2557-
ax.figure.canvas.draw()
2582+
ax.figure.canvas.draw_idle()
25582583
# toggle scaling of x-axes between 'log and 'linear' (default key 'k')
25592584
elif event.key in toggle_xscale_keys:
25602585
scalex = ax.get_xscale()
25612586
if scalex == 'log':
25622587
ax.set_xscale('linear')
2563-
ax.figure.canvas.draw()
2588+
ax.figure.canvas.draw_idle()
25642589
elif scalex == 'linear':
25652590
ax.set_xscale('log')
2566-
ax.figure.canvas.draw()
2591+
ax.figure.canvas.draw_idle()
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)