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

Skip to content

Turn off minor grids when interactively turning off major grids. #6995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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
Expand Down
15 changes: 10 additions & 5 deletions lib/matplotlib/backend_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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):
Expand Down