8000 Merge pull request #16823 from anntzer/toolgrid · matplotlib/matplotlib@1bf9ea5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1bf9ea5

Browse files
authored
Merge pull request #16823 from anntzer/toolgrid
MNT: Dedupe implementation of axes grid switching in toolmanager.
2 parents e9e1c87 + 3b63801 commit 1bf9ea5

File tree

1 file changed

+19
-63
lines changed

1 file changed

+19
-63
lines changed

lib/matplotlib/backend_tools.py

Lines changed: 19 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import re
1717
import time
1818
from types import SimpleNamespace
19+
import uuid
1920
from weakref import WeakKeyDictionary
2021

2122
import numpy as np
@@ -440,79 +441,34 @@ class ToolEnableNavigation(_ToolEnableNavigation):
440441
pass
441442

442443

443-
class _ToolGridBase(ToolBase):
444-
"""Common functionality between ToolGrid and ToolMinorGrid."""
445-
446-
_cycle = [(False, False), (True, False), (True, True), (False, True)]
447-
448-
def trigger(self, sender, event, data=None):
449-
ax = event.inaxes
450-
if ax is None:
451-
return
452-
try:
453-
x_state, x_which, y_state, y_which = self._get_next_grid_states(ax)
454-
except ValueError:
455-
pass
456-
else:
457-
ax.grid(x_state, which=x_which, axis="x")
458-
ax.grid(y_state, which=y_which, axis="y")
459-
ax.figure.canvas.draw_idle()
460-
461-
@staticmethod
462-
def _get_uniform_grid_state(ticks):
463-
"""
464-
Check whether all grid lines are in the same visibility state.
465-
466-
Returns True/False if all grid lines are on or off, None if they are
467-
not all in the same state.
468-
"""
469-
if all(tick.gridline.get_visible() for tick in ticks):
470-
return True
471-
elif not any(tick.gridline.get_visible() for tick in ticks):
472-
return False
473-
else:
474-
return None
475-
476-
477-
class ToolGrid(_ToolGridBase):
444+
class ToolGrid(ToolBase):
478445
"""Tool to toggle the major grids of the figure."""
479446

480447
description = 'Toggle major grids'
481448
default_keymap = mpl.rcParams['keymap.grid']
482449

483-
def _get_next_grid_states(self, ax):
484-
if None in map(self._get_uniform_grid_state,
485-
[ax.xaxis.minorTicks, ax.yaxis.minorTicks]):
486-
# Bail out if minor grids are not in a uniform state.
487-
raise ValueError
488-
x_state, y_state = map(self._get_uniform_grid_state,
489-
[ax.xaxis.majorTicks, ax.yaxis.majorTicks])
490-
cycle = self._cycle
491-
# Bail out (via ValueError) if major grids are not in a uniform state.
492-
x_state, y_state = (
493-
cycle[(cycle.index((x_state, y_state)) + 1) % len(cycle)])
494-
return (x_state, "major" if x_state else "both",
495-
y_state, "major" if y_state else "both")
496-
497-
498-
class ToolMinorGrid(_ToolGridBase):
450+
def trigger(self, sender, event, data=None):
451+
sentinel = str(uuid.uuid4())
452+
# Trigger grid switching by temporarily setting :rc:`keymap.grid`
453+
# to a unique key and sending an appropriate event.
454+
with cbook._setattr_cm(event, key=sentinel), \
455+
mpl.rc_context({'keymap.grid': sentinel}):
456+
mpl.backend_bases.key_press_handler(event, self.figure.canvas)
457+
458+
459+
class ToolMinorGrid(ToolBase):
499460
"""Tool to toggle the major and minor grids of the figure."""
500461

501462
description = 'Toggle major and minor grids'
502463
default_keymap = mpl.rcParams['keymap.grid_minor']
503464

504-
def _get_next_grid_states(self, ax):
505-
if None in map(self._get_uniform_grid_state,
506-
[ax.xaxis.majorTicks, ax.yaxis.majorTicks]):
507-
# Bail out if major grids are not in a uniform state.
508-
raise ValueError
509-
x_state, y_state = map(self._get_uniform_grid_state,
510-
[ax.xaxis.minorTicks, ax.yaxis.minorTicks])
511-
cycle = self._cycle
512-
# Bail out (via ValueError) if minor grids are not in a uniform state.
513-
x_state, y_state = (
514-
cycle[(cycle.index((x_state, y_state)) + 1) % len(cycle)])
515-
return x_state, "both", y_state, "both"
465+
def trigger(self, sender, event, data=None):
466+
sentinel = str(uuid.uuid4())
467+
# Trigger grid switching by temporarily setting :rc:`keymap.grid_minor`
468+
# to a unique key and sending an appropriate event.
469+
with cbook._setattr_cm(event, key=sentinel), \
470+
mpl.rc_context({'keymap.grid_minor': sentinel}):
471+
mpl.backend_bases.key_press_handler(event, self.figure.canvas)
516472

517473

518474
class ToolFullScreen(ToolToggleBase):

0 commit comments

Comments
 (0)
0