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

Fix toolmanager x/y scale "togglers". #26580

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
Loading
from
Draft
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
105 changes: 34 additions & 71 deletions 105 lib/matplotlib/backend_tools.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
"""
Abstract base classes define the primitives for Tools.
These tools are used by `matplotlib.backend_managers.ToolManager`

:class:`ToolBase`
Simple stateless tool
`ToolBase`
Simple stateless tool.

:class:`ToolToggleBase`
Tool that has two states, only one Toggle tool can be
active at any given time for the same
`matplotlib.backend_managers.ToolManager`
`ToolToggleBase`
Tool that has two states, only one Toggle tool can be active at any given time for
the same `matplotlib.backend_managers.ToolManager`.
"""

import enum
Expand All @@ -22,7 +21,6 @@
import numpy as np

import matplotlib as mpl
from matplotlib._pylab_helpers import Gcf
from matplotlib import _api, cbook


Expand Down Expand Up @@ -352,101 +350,66 @@
pass


class ToolQuit(ToolBase):
class _ToolForwardingToClassicToolbar(ToolBase):
_rc_entry = '' # Must be set by subclass.
default_keymap = property(lambda self: mpl.rcParams[self._rc_entry])

def trigger(self, sender, event, data=None):
sentinel = str(uuid.uuid4())
# Trigger classic key press event handling by temporarily setting the keymap to
# a unique key and sending an event with that key.
with (cbook._setattr_cm(event, key=sentinel),
mpl.rc_context({self._rc_entry: sentinel})):
mpl.backend_bases.key_press_handler(event, self.figure.canvas)


class ToolQuit(_ToolForwardingToClassicToolbar):
"""Tool to call the figure manager destroy method."""

description = 'Quit the figure'
default_keymap = property(lambda self: mpl.rcParams['keymap.quit'])

def trigger(self, sender, event, data=None):
Gcf.destroy_fig(self.figure)
_rc_entry = 'keymap.quit'


class ToolQuitAll(ToolBase):
class ToolQuitAll(_ToolForwardingToClassicToolbar):
"""Tool to call the figure manager destroy method."""

description = 'Quit all figures'
default_keymap = property(lambda self: mpl.rcParams['keymap.quit_all'])

def trigger(self, sender, event, data=None):
Gcf.destroy_all()
_rc_entry = 'keymap.quit_all'


class ToolGrid(ToolBase):
class ToolGrid(_ToolForwardingToClassicToolbar):
"""Tool to toggle the major grids of the figure."""

description = 'Toggle major grids'
default_keymap = property(lambda self: mpl.rcParams['keymap.grid'])

def trigger(self, sender, event, data=None):
sentinel = str(uuid.uuid4())
# Trigger grid switching by temporarily setting :rc:`keymap.grid`
# to a unique key and sending an appropriate event.
with (cbook._setattr_cm(event, key=sentinel),
mpl.rc_context({'keymap.grid': sentinel})):
mpl.backend_bases.key_press_handler(event, self.figure.canvas)
_rc_entry = 'keymap.grid'


class ToolMinorGrid(ToolBase):
class ToolMinorGrid(_ToolForwardingToClassicToolbar):
"""Tool to toggle the major and minor grids of the figure."""

description = 'Toggle major and minor grids'
default_keymap = property(lambda self: mpl.rcParams['keymap.grid_minor'])

def trigger(self, sender, event, data=None):
sentinel = str(uuid.uuid4())
# Trigger grid switching by temporarily setting :rc:`keymap.grid_minor`
# to a unique key and sending an appropriate event.
with (cbook._setattr_cm(event, key=sentinel),
mpl.rc_context({'keymap.grid_minor': sentinel})):
mpl.backend_bases.key_press_handler(event, self.figure.canvas)
_rc_entry = 'keymap.grid_minor'


class ToolFullScreen(ToolBase):
class ToolFullScreen(_ToolForwardingToClassicToolbar):
"""Tool to toggle full screen."""

description = 'Toggle fullscreen mode'
default_keymap = property(lambda self: mpl.rcParams['keymap.fullscreen'])
_rc_entry = 'keymap.fullscreen'

def trigger(self, sender, event, data=None):
self.figure.canvas.manager.full_screen_toggle()


class AxisScaleBase(ToolToggleBase):
"""Base Tool to toggle between linear and logarithmic."""

def trigger(self, sender, event, data=None):
if event.inaxes is None:
return
super().trigger(sender, event, data)

def enable(self, event=None):
self.set_scale(event.inaxes, 'log')
self.figure.canvas.draw_idle()
class ToolXScale(_ToolForwardingToClassicToolbar):
"""Tool to toggle between linear and logarithmic scales on the X axis."""

def disable(self, event=None):
self.set_scale(event.inaxes, 'linear')
self.figure.canvas.draw_idle()
description = 'Toggle scale X axis'
_rc_entry = 'keymap.xscale'


class ToolYScale(AxisScaleBase):
class ToolYScale(_ToolForwardingToClassicToolbar):
"""Tool to toggle between linear and logarithmic scales on the Y axis."""

description = 'Toggle scale Y axis'
default_keymap = property(lambda self: mpl.rcParams['keymap.yscale'])

def set_scale(self, ax, scale):
ax.set_yscale(scale)


class ToolXScale(AxisScaleBase):
"""Tool to toggle between linear and logarithmic scales on the X axis."""

description = 'Toggle scale X axis'
default_keymap = property(lambda self: mpl.rcParams['keymap.xscale'])

def set_scale(self, ax, scale):
ax.set_xscale(scale)
_rc_entry = 'keymap.yscale'


class ToolViewsPositions(ToolBase):
Expand Down
25 changes: 10 additions & 15 deletions 25 lib/matplotlib/backend_tools.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,16 @@ class RubberbandBase(ToolBase):
def draw_rubberband(self, *data) -> None: ...
def remove_rubberband(self) -> None: ...

class ToolQuit(ToolBase): ...
class ToolQuitAll(ToolBase): ...
class ToolGrid(ToolBase): ...
class ToolMinorGrid(ToolBase): ...
class ToolFullScreen(ToolBase): ...

class AxisScaleBase(ToolToggleBase):
def enable(self, event: ToolEvent | None = ...) -> None: ...
def disable(self, event: ToolEvent | None = ...) -> None: ...

class ToolYScale(AxisScaleBase):
def set_scale(self, ax: Axes, scale: str | ScaleBase) -> None: ...

class ToolXScale(AxisScaleBase):
def set_scale(self, ax, scale: str | ScaleBase) -> None: ...
class _ToolForwardingToClassicToolbar(ToolBase):
_rc_entry: str

class ToolQuit(_ToolForwardingToClassicToolbar): ...
class ToolQuitAll(_ToolForwardingToClassicToolbar): ...
class ToolGrid(_ToolForwardingToClassicToolbar): ...
class ToolMinorGrid(_ToolForwardingToClassicToolbar): ...
class ToolFullScreen(_ToolForwardingToClassicToolbar): ...
class ToolXScale(_ToolForwardingToClassicToolbar): ...
class ToolYScale(_ToolForwardingToClassicToolbar): ...

class ToolViewsPositions(ToolBase):
views: dict[Figure | Axes, cbook._Stack]
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.