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

Skip to content

Commit 735cbde

Browse files
committed
Turn off minor grids when interactively turning off major grids.
1 parent a9c9012 commit 735cbde

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,9 +2541,13 @@ def _get_uniform_gridstate(ticks):
25412541

25422542
ax = event.inaxes
25432543
# toggle major grids in current axes (default key 'g')
2544-
# Both here and below (for 'G'), we do nothing is the grids are not in a
2545-
# uniform state, to avoid messing up user customization.
2546-
if event.key in grid_keys:
2544+
# Both here and below (for 'G'), we do nothing if *any* grid (major or
2545+
# minor, x or y) is not in a uniform state, to avoid messing up user
2546+
# customization.
2547+
if (event.key in grid_keys
2548+
# Exclude minor grids not in a uniform state.
2549+
and None not in [_get_uniform_gridstate(ax.xaxis.minorTicks),
2550+
_get_uniform_gridstate(ax.yaxis.minorTicks)]):
25472551
x_state = _get_uniform_gridstate(ax.xaxis.majorTicks)
25482552
y_state = _get_uniform_gridstate(ax.yaxis.majorTicks)
25492553
cycle = [(False, False), (True, False), (True, True), (False, True)]
@@ -2554,8 +2558,9 @@ def _get_uniform_gridstate(ticks):
25542558
# Exclude major grids not in a uniform state.
25552559
pass
25562560
else:
2557-
ax.grid(x_state, which="major", axis="x")
2558-
ax.grid(y_state, which="major", axis="y")
2561+
# If turning major grids off, also turn minor grids off.
2562+
ax.grid(x_state, which="major" if x_state else "both", axis="x")
2563+
ax.grid(y_state, which="major" if y_state else "both", axis="y")
25592564
canvas.draw_idle()
25602565
# toggle major and minor grids in current axes (default key 'G')
25612566
if (event.key in grid_minor_keys

lib/matplotlib/backend_tools.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,12 @@ def trigger(self, sender, event, data=None):
430430
if ax is None:
431431
return
432432
try:
433-
x_state, y_state, which = self._get_next_grid_states(ax)
433+
x_state, x_which, y_state, y_which = self._get_next_grid_states(ax)
434434
except ValueError:
435435
pass
436436
else:
437-
ax.grid(x_state, which=which, axis="x")
438-
ax.grid(y_state, which=which, axis="y")
437+
ax.grid(x_state, which=x_which, axis="x")
438+
ax.grid(y_state, which=y_which, axis="y")
439439
ax.figure.canvas.draw_idle()
440440

441441
@staticmethod
@@ -461,13 +461,18 @@ class ToolGrid(_ToolGridBase):
461461
default_keymap = rcParams['keymap.grid']
462462

463463
def _get_next_grid_states(self, ax):
464+
if None in map(self._get_uniform_grid_state,
465+
[ax.xaxis.minorTicks, ax.yaxis.minorTicks]):
466+
# Bail out if minor grids are not in a uniform state.
467+
raise ValueError
464468
x_state, y_state = map(self._get_uniform_grid_state,
465469
[ax.xaxis.majorTicks, ax.yaxis.majorTicks])
466470
cycle = self._cycle
467471
# Bail out (via ValueError) if major grids are not in a uniform state.
468472
x_state, y_state = (
469473
cycle[(cycle.index((x_state, y_state)) + 1) % len(cycle)])
470-
return x_state, y_state, "major"
474+
return (x_state, "major" if x_state else "both",
475+
y_state, "major" if y_state else "both")
471476

472477

473478
class ToolMinorGrid(_ToolGridBase):
@@ -487,7 +492,7 @@ def _get_next_grid_states(self, ax):
487492
# Bail out (via ValueError) if minor grids are not in a uniform state.
488493
x_state, y_state = (
489494
cycle[(cycle.index((x_state, y_state)) + 1) % len(cycle)])
490-
return x_state, y_state, "both"
495+
return x_state, "both", y_state, "both"
491496

492497

493498
class ToolFullScreen(ToolToggleBase):

0 commit comments

Comments
 (0)