diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index dbd879521f25..ad39f69be439 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -928,11 +928,15 @@ def _break_share_link(ax, grouper): self.stale = True self._localaxes.remove(ax) + # Break link between any shared axes for name in ax._axis_names: last_ax = _break_share_link(ax, ax._shared_axes[name]) if last_ax is not None: _reset_locators_and_formatters(getattr(last_ax, f"{name}axis")) + # Break link between any twinned axes + _break_share_link(ax, ax._twinned_axes) + # Note: in the docstring below, the newlines in the examples after the # calls to legend() allow replacing it with figlegend() to generate the # docstring of pyplot.figlegend. diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 32b0c202478b..6067d54cf517 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -4663,6 +4663,21 @@ def test_spectrum(): ax.set(xlabel="", ylabel="") +@check_figures_equal(extensions=['png']) +def test_twin_remove(fig_test, fig_ref): + ax_test = fig_test.add_subplot() + ax_twinx = ax_test.twinx() + ax_twiny = ax_test.twiny() + ax_twinx.remove() + ax_twiny.remove() + + ax_ref = fig_ref.add_subplot() + # Ideally we also undo tick changes when calling ``remove()``, but for now + # manually set the ticks of the reference image to match the test image + ax_ref.xaxis.tick_bottom() + ax_ref.yaxis.tick_left() + + @image_comparison(['twin_spines.png'], remove_text=True) def test_twin_spines():