Skip to content

Backport PR #21076 on branch v3.5.x (Break links between twinned axes when removing) #21077

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
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
4 changes: 4 additions & 0 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():

Expand Down