Skip to content

Fix pickling of AxesWidgets. #28430

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
merged 5 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,11 @@ def test_cycler():
ax = pickle.loads(pickle.dumps(ax))
l, = ax.plot([3, 4])
assert l.get_color() == "m"


# Run under an interactive backend to test that we don't try to pickle the
# (interactive and non-picklable) canvas.
@pytest.mark.backend('tkagg')
def test_axeswidget_interactive():
ax = plt.figure().add_subplot()
pickle.dumps(mpl.widgets.Button(ax, "button"))
35 changes: 8 additions & 27 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,6 @@ def ignore(self, event):
"""
return not self.active

def _changed_canvas(self):
"""
Someone has switched the canvas on us!

This happens if `savefig` needs to save to a format the previous
backend did not support (e.g. saving a figure using an Agg based
backend saved to a vector format).

Returns
-------
bool
True if the canvas has been changed.

"""
return self.canvas is not self.ax.figure.canvas


class AxesWidget(Widget):
"""
Expand All @@ -131,9 +115,10 @@ class AxesWidget(Widget):

def __init__(self, ax):
self.ax = ax
self.canvas = ax.figure.canvas
self._cids = []

canvas = property(lambda self: self.ax.figure.canvas)

def connect_event(self, event, callback):
"""
Connect a callback function with an event.
Expand Down Expand Up @@ -1100,7 +1085,7 @@ def __init__(self, ax, labels, actives=None, *, useblit=True,

def _clear(self, event):
"""Internal event handler to clear the buttons."""
if self.ignore(event) or self._changed_canvas():
if self.ignore(event) or self.canvas.is_saving():
return
self._background = self.canvas.copy_from_bbox(self.ax.bbox)
self.ax.draw_artist(self._checks)
Expand Down Expand Up @@ -1677,7 +1662,7 @@ def __init__(self, ax, labels, active=0, activecolor=None, *,

def _clear(self, event):
"""Internal event handler to clear the buttons."""
if self.ignore(event) or self._changed_canvas():
if self.ignore(event) or self.canvas.is_saving():
return
self._background = self.canvas.copy_from_bbox(self.ax.bbox)
self.ax.draw_artist(self._buttons)
Expand Down Expand Up @@ -1933,7 +1918,7 @@ def __init__(self, ax, *, horizOn=True, vertOn=True, useblit=False,

def clear(self, event):
"""Internal event handler to clear the cursor."""
if self.ignore(event) or self._changed_canvas():
if self.ignore(event) or self.canvas.is_saving():
return
if self.useblit:
self.background = self.canvas.copy_from_bbox(self.ax.bbox)
Expand Down Expand Up @@ -2573,9 +2558,7 @@ def __init__(self, ax, onselect, direction, *, minspan=0, useblit=False,
self.drag_from_anywhere = drag_from_anywhere
self.ignore_event_outside = ignore_event_outside

# Reset canvas so that `new_axes` connects events.
self.canvas = None
self.new_axes(ax, _props=props)
self.new_axes(ax, _props=props, _init=True)

# Setup handles
self._handle_props = {
Expand All @@ -2588,14 +2571,12 @@ def __init__(self, ax, onselect, direction, *, minspan=0, useblit=False,

self._active_handle = None

def new_axes(self, ax, *, _props=None):
def new_axes(self, ax, *, _props=None, _init=False):
"""Set SpanSelector to operate on a new Axes."""
self.ax = ax
if self.canvas is not ax.figure.canvas:
if _init or self.canvas is not ax.figure.canvas:
if self.canvas is not None:
self.disconnect_events()

self.canvas = ax.figure.canvas
self.connect_default_events()

# Reset
Expand Down
12 changes: 9 additions & 3 deletions lib/matplotlib/widgets.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ class Widget:

class AxesWidget(Widget):
ax: Axes
canvas: FigureCanvasBase | None
def __init__(self, ax: Axes) -> None: ...
@property
def canvas(self) -> FigureCanvasBase | None: ...
def connect_event(self, event: Event, callback: Callable) -> None: ...
def disconnect_events(self) -> None: ...

Expand Down Expand Up @@ -310,7 +311,6 @@ class SpanSelector(_SelectorWidget):
grab_range: float
drag_from_anywhere: bool
ignore_event_outside: bool
canvas: FigureCanvasBase | None
def __init__(
self,
ax: Axes,
Expand All @@ -330,7 +330,13 @@ class SpanSelector(_SelectorWidget):
ignore_event_outside: bool = ...,
snap_values: ArrayLike | None = ...,
) -> None: ...
def new_axes(self, ax: Axes, *, _props: dict[str, Any] | None = ...) -> None: ...
def new_axes(
self,
ax: Axes,
*,
_props: dict[str, Any] | None = ...,
_init: bool = ...,
) -> None: ...
def connect_default_events(self) -> None: ...
@property
def direction(self) -> Literal["horizontal", "vertical"]: ...
Expand Down
Loading