Skip to content

Commit

Permalink
Backport PR matplotlib#29249: [Bug Fix] Fix reverse mapping for _tran…
Browse files Browse the repository at this point in the history
…slate_tick_params
  • Loading branch information
kyracho authored and meeseeksmachine committed Dec 8, 2024
1 parent 7d3187d commit 4ca5d5e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'})

0 comments on commit 4ca5d5e

Please sign in to comment.