Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1fd07ae

Browse filesBrowse files
committed
Toolmanager implementation of minor grid toggler.
1 parent 0c3937c commit 1fd07ae
Copy full SHA for 1fd07ae

File tree

Expand file treeCollapse file tree

1 file changed

+64
-12
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+64
-12
lines changed

‎lib/matplotlib/backend_tools.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backend_tools.py
+64-12Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -399,24 +399,75 @@ def trigger(self, sender, event, data=None):
399399
a.set_navigate(i == n)
400400

401401

402-
class ToolGrid(ToolToggleBase):
403-
"""Tool to toggle the grid of the figure"""
402+
class _ToolGridBase(ToolBase):
403+
"""Common functionality between ToolGrid and ToolMinorGrid.
404+
"""
404405

405-
description = 'Toogle Grid'
406-
default_keymap = rcParams['keymap.grid']
406+
_cycle = [(False, False), (True, False), (True, True), (False, True)]
407407

408408
def trigger(self, sender, event, data=None):
409-
if event.inaxes is None:
409+
ax = event.inaxes
410+
if ax is None:
410411
return
411-
ToolToggleBase.trigger(self, sender, event, data)
412+
try:
413+
x_state, y_state, which = self._get_next_grid_states(ax)
414+
except ValueError:
415+
pass
416+
else:
417+
ax.grid(x_state, which=which, axis="x")
418+
ax.grid(y_state, which=which, axis="y")
419+
ax.figure.canvas.draw_idle()
412420

413-
def enable(self, event):
414-
event.inaxes.grid(True)
415-
self.figure.canvas.draw_idle()
421+
@staticmethod
422+
def _get_uniform_grid_state(ticks):
423+
"""Check whether all grid lines are in the same visibility state.
416424
417-
def disable(self, event):
418-
event.inaxes.grid(False)
419-
self.figure.canvas.draw_idle()
425+
Returns True/False if all grid lines are on or off, None if they are
426+
not all in the same state.
427+
"""
428+
if all(tick.gridOn for tick in ticks):
429+
return True
430+
elif not any(tick.gridOn for tick in ticks):
431+
return False
432+
else:
433+
return None
434+
435+
436+
437+
class ToolGrid(_ToolGridBase):
438+
"""Tool to toggle the grid of the figure"""
439+
440+
description = 'Toogle major grids'
441+
default_keymap = rcParams['keymap.grid']
442+
443+
def _get_next_grid_states(self, ax):
444+
x_state, y_state = map(self._get_uniform_grid_state,
445+
[ax.xaxis.majorTicks, ax.yaxis.majorTicks])
446+
cycle = self._cycle
447+
# Bail out if major grids are not in a uniform state.
448+
x_state, y_state = (
449+
cycle[(cycle.index((x_state, y_state)) + 1) % len(cycle)])
450+
return x_state, y_state, "major"
451+
452+
453+
class ToolMinorGrid(_ToolGridBase):
454+
"""Tool to toggle the grid of the figure"""
455+
456+
description = 'Toogle major and minor grids'
457+
default_keymap = rcParams['keymap.grid_minor']
458+
459+
def _get_next_grid_states(self, ax):
460+
if None in map(self._get_uniform_grid_state,
461+
[ax.xaxis.majorTicks, ax.yaxis.majorTicks]):
462+
# Bail out if major grids are not in a uniform state.
463+
raise ValueError
464+
x_state, y_state = map(self._get_uniform_grid_state,
465+
[ax.xaxis.minorTicks, ax.yaxis.minorTicks])
466+
cycle = self._cycle
467+
# Bail out if minor grids are not in a uniform state.
468+
x_state, y_state = (
469+
cycle[(cycle.index((x_state, y_state)) + 1) % len(cycle)])
470+
return x_state, y_state, "both"
420471

421472

422473
class ToolFullScreen(ToolToggleBase):
@@ -944,6 +995,7 @@ def _mouse_move(self, event):
944995
'subplots': 'ToolConfigureSubplots',
945996
'save': 'ToolSaveFigure',
946997
'grid': ToolGrid,
998+
'grid_minor': ToolMinorGrid,
947999
'fullscreen': ToolFullScreen,
9481000
'quit': ToolQuit,
9491001
'quit_all': ToolQuitAll,

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.