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

Skip to content

Properly disconnect machinery when removing child axes. #26614

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 1 commit into from
Aug 28, 2023
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
3 changes: 2 additions & 1 deletion lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2237,7 +2237,8 @@ def add_child_axes(self, ax):
ax.stale_callback = martist._stale_axes_callback

self.child_axes.append(ax)
ax._remove_method = self.child_axes.remove
ax._remove_method = functools.partial(
self.figure._remove_axes, owners=[self.child_axes])
self.stale = True
return ax

Expand Down
18 changes: 16 additions & 2 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,11 +936,25 @@ def delaxes(self, ax):
"""
Remove the `~.axes.Axes` *ax* from the figure; update the current Axes.
"""
self._remove_axes(ax, owners=[self._axstack, self._localaxes])

def _remove_axes(self, ax, owners):
"""
Common helper for removal of standard axes (via delaxes) and of child axes.

Parameters
----------
ax : `~.AxesBase`
The Axes to remove.
owners
List of objects (list or _AxesStack) "owning" the axes, from which the Axes
will be remove()d.
"""
for owner in owners:
owner.remove(ax)

self._axstack.remove(ax)
self._axobservers.process("_axes_change_event", self)
self.stale = True
self._localaxes.remove(ax)
self.canvas.release_mouse(ax)

for name in ax._axis_names: # Break link between any shared axes
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8679,6 +8679,14 @@ def test_cla_clears_children_axes_and_fig():
assert art.figure is None


def test_child_axes_removal():
fig, ax = plt.subplots()
marginal = ax.inset_axes([1, 0, .1, 1], sharey=ax)
marginal_twin = marginal.twinx()
marginal.remove()
ax.set(xlim=(-1, 1), ylim=(10, 20))


def test_scatter_color_repr_error():

def get_next_color():
Expand Down