Skip to content

Commit e63f885

Browse files
committed
Cleanup Axis._translate_tick_kw
This keeps the exact logic and only rewrites it in a more readable way. Some more refactoring would be possible, that would clean up the logic itself a bit more, but I'll leave that for some later time.
1 parent 4784ffa commit e63f885

File tree

1 file changed

+42
-33
lines changed

1 file changed

+42
-33
lines changed

lib/matplotlib/axis.py

+42-33
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ def set_tick_params(self, which='major', reset=False, **kwargs):
855855
:meth:`matplotlib.axes.Axes.tick_params`.
856856
"""
857857
_api.check_in_list(['major', 'minor', 'both'], which=which)
858-
kwtrans = self._translate_tick_kw(kwargs)
858+
kwtrans = self._translate_tick_params(kwargs)
859859

860860
# the kwargs are stored in self._major/minor_tick_kw so that any
861861
# future new ticks will automatically get them
@@ -887,47 +887,56 @@ def set_tick_params(self, which='major', reset=False, **kwargs):
887887
self.stale = True
888888

889889
@staticmethod
890-
def _translate_tick_kw(kw):
890+
def _translate_tick_params(kw):
891+
"""
892+
Translate the kwargs supported by `.Axis.set_tick_params` to kwargs
893+
supported by `.Tick._apply_params`.
894+
895+
In particular, this maps axis specific names like 'top', 'left'
896+
to the generic tick1, tick2 logic of the axis. Additionally, there
897+
are some other name translations.
898+
899+
Returns a new dict of translated kwargs.
900+
901+
Note: The input *kwargs* are currently modified, but that's ok for
902+
the only caller.
903+
"""
891904
# The following lists may be moved to a more accessible location.
892-
kwkeys = ['size', 'width', 'color', 'tickdir', 'pad',
893-
'labelsize', 'labelcolor', 'zorder', 'gridOn',
894-
'tick1On', 'tick2On', 'label1On', 'label2On',
895-
'length', 'direction', 'left', 'bottom', 'right', 'top',
896-
'labelleft', 'labelbottom', 'labelright', 'labeltop',
897-
'labelrotation'] + _gridline_param_names
898-
kwtrans = {}
899-
if 'length' in kw:
900-
kwtrans['size'] = kw.pop('length')
901-
if 'direction' in kw:
902-
kwtrans['tickdir'] = kw.pop('direction')
903-
if 'rotation' in kw:
904-
kwtrans['labelrotation'] = kw.pop('rotation')
905-
if 'left' in kw:
906-
kwtrans['tick1On'] = kw.pop('left')
907-
if 'bottom' in kw:
908-
kwtrans['tick1On'] = kw.pop('bottom')
909-
if 'right' in kw:
910-
kwtrans['tick2On'] = kw.pop('right')
911-
if 'top' in kw:
912-
kwtrans['tick2On'] = kw.pop('top')
913-
if 'labelleft' in kw:
914-
kwtrans['label1On'] = kw.pop('labelleft')
915-
if 'labelbottom' in kw:
916-
kwtrans['label1On'] = kw.pop('labelbottom')
917-
if 'labelright' in kw:
918-
kwtrans['label2On'] = kw.pop('labelright')
919-
if 'labeltop' in kw:
920-
kwtrans['label2On'] = kw.pop('labeltop')
905+
allowed_keys = [
906+
'size', 'width', 'color', 'tickdir', 'pad',
907+
'labelsize', 'labelcolor', 'zorder', 'gridOn',
908+
'tick1On', 'tick2On', 'label1On', 'label2On',
909+
'length', 'direction', 'left', 'bottom', 'right', 'top',
910+
'labelleft', 'labelbottom', 'labelright', 'labeltop',
911+
'labelrotation',
912+
*_gridline_param_names]
913+
914+
keymap = {
915+
# tick_params key -> axis key
916+
'length': 'size',
917+
'direction': 'tickdir',
918+
'rotation': 'labelrotation',
919+
'left': 'tick1On',
920+
'bottom': 'tick1On',
921+
'right': 'tick2On',
922+
'top': 'tick2On',
923+
'labelleft': 'label1On',
924+
'labelbottom': 'label1On',
925+
'labelright': 'label2On',
926+
'labeltop': 'label2On',
927+
}
928+
kwtrans = {newkey: kw.pop(oldkey)
929+
for oldkey, newkey in keymap.items() if oldkey in kw}
921930
if 'colors' in kw:
922931
c = kw.pop('colors')
923932
kwtrans['color'] = c
924933
kwtrans['labelcolor'] = c
925934
# Maybe move the checking up to the caller of this method.
926935
for key in kw:
927-
if key not in kwkeys:
936+
if key not in allowed_keys:
928937
raise ValueError(
929938
"keyword %s is not recognized; valid keywords are %s"
930-
% (key, kwkeys))
939+
% (key, allowed_keys))
931940
kwtrans.update(kw)
932941
return kwtrans
933942

0 commit comments

Comments
 (0)