From 735cbdef32646a460ff2f1ec1cecb0ea342c8f4c Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 28 Aug 2016 16:53:58 -0700 Subject: [PATCH] Turn off minor grids when interactively turning off major grids. --- lib/matplotlib/backend_bases.py | 15 ++++++++++----- lib/matplotlib/backend_tools.py | 15 ++++++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 3effb393cd85..1f5a76eec5ae 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2541,9 +2541,13 @@ def _get_uniform_gridstate(ticks): ax = event.inaxes # toggle major grids in current axes (default key 'g') - # Both here and below (for 'G'), we do nothing is the grids are not in a - # uniform state, to avoid messing up user customization. - if event.key in grid_keys: + # Both here and below (for 'G'), we do nothing if *any* grid (major or + # minor, x or y) is not in a uniform state, to avoid messing up user + # customization. + if (event.key in grid_keys + # Exclude minor grids not in a uniform state. + and None not in [_get_uniform_gridstate(ax.xaxis.minorTicks), + _get_uniform_gridstate(ax.yaxis.minorTicks)]): x_state = _get_uniform_gridstate(ax.xaxis.majorTicks) y_state = _get_uniform_gridstate(ax.yaxis.majorTicks) cycle = [(False, False), (True, False), (True, True), (False, True)] @@ -2554,8 +2558,9 @@ def _get_uniform_gridstate(ticks): # Exclude major grids not in a uniform state. pass else: - ax.grid(x_state, which="major", axis="x") - ax.grid(y_state, which="major", axis="y") + # If turning major grids off, also turn minor grids off. + ax.grid(x_state, which="major" if x_state else "both", axis="x") + ax.grid(y_state, which="major" if y_state else "both", axis="y") canvas.draw_idle() # toggle major and minor grids in current axes (default key 'G') if (event.key in grid_minor_keys diff --git a/lib/matplotlib/backend_tools.py b/lib/matplotlib/backend_tools.py index c79a0821115e..83b5689f76a4 100644 --- a/lib/matplotlib/backend_tools.py +++ b/lib/matplotlib/backend_tools.py @@ -430,12 +430,12 @@ def trigger(self, sender, event, data=None): if ax is None: return try: - x_state, y_state, which = self._get_next_grid_states(ax) + x_state, x_which, y_state, y_which = self._get_next_grid_states(ax) except ValueError: pass else: - ax.grid(x_state, which=which, axis="x") - ax.grid(y_state, which=which, axis="y") + ax.grid(x_state, which=x_which, axis="x") + ax.grid(y_state, which=y_which, axis="y") ax.figure.canvas.draw_idle() @staticmethod @@ -461,13 +461,18 @@ class ToolGrid(_ToolGridBase): default_keymap = rcParams['keymap.grid'] def _get_next_grid_states(self, ax): + if None in map(self._get_uniform_grid_state, + [ax.xaxis.minorTicks, ax.yaxis.minorTicks]): + # Bail out if minor grids are not in a uniform state. + raise ValueError x_state, y_state = map(self._get_uniform_grid_state, [ax.xaxis.majorTicks, ax.yaxis.majorTicks]) cycle = self._cycle # Bail out (via ValueError) if major grids are not in a uniform state. x_state, y_state = ( cycle[(cycle.index((x_state, y_state)) + 1) % len(cycle)]) - return x_state, y_state, "major" + return (x_state, "major" if x_state else "both", + y_state, "major" if y_state else "both") class ToolMinorGrid(_ToolGridBase): @@ -487,7 +492,7 @@ def _get_next_grid_states(self, ax): # Bail out (via ValueError) if minor grids are not in a uniform state. x_state, y_state = ( cycle[(cycle.index((x_state, y_state)) + 1) % len(cycle)]) - return x_state, y_state, "both" + return x_state, "both", y_state, "both" class ToolFullScreen(ToolToggleBase):