From 877b6301062d841820129200db315007b9be4070 Mon Sep 17 00:00:00 2001 From: Jason King Date: Thu, 30 May 2024 20:50:49 +1000 Subject: [PATCH] A twinx-copied axis with a right-aligned title will remain right --- lib/matplotlib/axes/_base.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 980ef2f51c94..433647516f91 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -1261,6 +1261,14 @@ def __clear(self): # deprecation on cla() subclassing expires. # stash the current visibility state + + # A twinx-copied axis title set to the right will move back the + # left if it is cleared. + set_right_label_again = False + if 'y' in self._axis_map: + set_right_label_again = \ + (self._axis_map['y'].get_label_position() == 'right') + if hasattr(self, 'patch'): patch_visible = self.patch.get_visible() else: @@ -1378,6 +1386,9 @@ def __clear(self): axis._set_lim(0, 1, auto=True) self._update_transScale() + if set_right_label_again: + self._axis_map['y'].set_label_position('right') + self.stale = True def clear(self):