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

Skip to content

Commit bd0da74

Browse files
committed
Properly disconnect machinery when removing child axes.
1 parent 0ac170b commit bd0da74

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

lib/matplotlib/axes/_base.py

Lines 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

Lines 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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8679,6 +8679,14 @@ def test_cla_clears_children_axes_and_fig():
86798679
assert art.figure is None
86808680

86818681

8682+
def test_child_axes_removal():
8683+
fig, ax = plt.subplots()
8684+
marginal = ax.inset_axes([1, 0, .1, 1], sharey=ax)
8685+
marginal_twin = marginal.twinx()
8686+
marginal.remove()
8687+
ax.set(xlim=(-1, 1), ylim=(10, 20))
8688+
8689+
86828690
def test_scatter_color_repr_error():
86838691

86848692
def get_next_color():

0 commit comments

Comments
 (0)