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 a20099c

Browse filesBrowse files
authored
Merge pull request #26614 from anntzer/rca
Properly disconnect machinery when removing child axes.
2 parents a40d22b + bd0da74 commit a20099c
Copy full SHA for a20099c

File tree

3 files changed

+26
-3
lines changed
Filter options

3 files changed

+26
-3
lines changed

‎lib/matplotlib/axes/_base.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_base.py
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2237,7 +2237,8 @@ def add_child_axes(self, ax):
22372237
ax.stale_callback = martist._stale_axes_callback
22382238

22392239
self.child_axes.append(ax)
2240-
ax._remove_method = self.child_axes.remove
2240+
ax._remove_method = functools.partial(
2241+
self.figure._remove_axes, owners=[self.child_axes])
22412242
self.stale = True
22422243
return ax
22432244

‎lib/matplotlib/figure.py

Copy file name to clipboardExpand all lines: lib/matplotlib/figure.py
+16-2Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -936,11 +936,25 @@ def delaxes(self, ax):
936936
"""
937937
Remove the `~.axes.Axes` *ax* from the figure; update the current Axes.
938938
"""
939+
self._remove_axes(ax, owners=[self._axstack, self._localaxes])
940+
941+
def _remove_axes(self, ax, owners):
942+
"""
943+
Common helper for removal of standard axes (via delaxes) and of child axes.
944+
945+
Parameters
946+
----------
947+
ax : `~.AxesBase`
948+
The Axes to remove.
949+
owners
950+
List of objects (list or _AxesStack) "owning" the axes, from which the Axes
951+
will be remove()d.
952+
"""
953+
for owner in owners:
954+
owner.remove(ax)
939955

940-
self._axstack.remove(ax)
941956
self._axobservers.process("_axes_change_event", self)
942957
self.stale = True
943-
self._localaxes.remove(ax)
944958
self.canvas.release_mouse(ax)
945959

946960
for name in ax._axis_names: # Break link between any shared axes

‎lib/matplotlib/tests/test_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_axes.py
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8687,6 +8687,14 @@ def test_cla_clears_children_axes_and_fig():
86878687
assert art.figure is None
86888688

86898689

8690+
def test_child_axes_removal():
8691+
fig, ax = plt.subplots()
8692+
marginal = ax.inset_axes([1, 0, .1, 1], sharey=ax)
8693+
marginal_twin = marginal.twinx()
8694+
marginal.remove()
8695+
ax.set(xlim=(-1, 1), ylim=(10, 20))
8696+
8697+
86908698
def test_scatter_color_repr_error():
86918699

86928700
def get_next_color():

0 commit comments

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