From 4ca5d5e896d49245bc63f2e7aa606d322999b336 Mon Sep 17 00:00:00 2001 From: Kyra Cho Date: Sun, 8 Dec 2024 00:18:40 -0800 Subject: [PATCH] Backport PR #29249: [Bug Fix] Fix reverse mapping for _translate_tick_params --- lib/matplotlib/axis.py | 17 +++++++++++------ lib/matplotlib/tests/test_axis.py | 9 +++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index dc816eccc30a..56eeb0e4169b 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1053,8 +1053,8 @@ def get_tick_params(self, which='major'): ) return self._translate_tick_params(self._minor_tick_kw, reverse=True) - @staticmethod - def _translate_tick_params(kw, reverse=False): + @classmethod + def _translate_tick_params(cls, kw, reverse=False): """ Translate the kwargs supported by `.Axis.set_tick_params` to kwargs supported by `.Tick._apply_params`. @@ -1096,10 +1096,15 @@ def _translate_tick_params(kw, reverse=False): 'labeltop': 'label2On', } if reverse: - kwtrans = { - oldkey: kw_.pop(newkey) - for oldkey, newkey in keymap.items() if newkey in kw_ - } + kwtrans = {} + is_x_axis = cls.axis_name == 'x' + y_axis_keys = ['left', 'right', 'labelleft', 'labelright'] + for oldkey, newkey in keymap.items(): + if newkey in kw_: + if is_x_axis and oldkey in y_axis_keys: + continue + else: + kwtrans[oldkey] = kw_.pop(newkey) else: kwtrans = { newkey: kw_.pop(oldkey) diff --git a/lib/matplotlib/tests/test_axis.py b/lib/matplotlib/tests/test_axis.py index 33af30662a33..c1b09006607d 100644 --- a/lib/matplotlib/tests/test_axis.py +++ b/lib/matplotlib/tests/test_axis.py @@ -29,3 +29,12 @@ def test_axis_not_in_layout(): # Positions should not be affected by overlapping 100 label assert ax1_left.get_position().bounds == ax2_left.get_position().bounds assert ax1_right.get_position().bounds == ax2_right.get_position().bounds + + +def test_translate_tick_params_reverse(): + fig, ax = plt.subplots() + kw = {'label1On': 'a', 'label2On': 'b', 'tick1On': 'c', 'tick2On': 'd'} + assert (ax.xaxis._translate_tick_params(kw, reverse=True) == + {'labelbottom': 'a', 'labeltop': 'b', 'bottom': 'c', 'top': 'd'}) + assert (ax.yaxis._translate_tick_params(kw, reverse=True) == + {'labelleft': 'a', 'labelright': 'b', 'left': 'c', 'right': 'd'})