Skip to content

Commit f15b8d5

Browse files
committed
Better validate tick direction.
- Validate rcParams["{x,y}tick.direction"]. - Move common validation and application to base Tick.apply_tickdir class. Note that previously YTick.apply_tickdir failed to _check_in_list the tickdir value.
1 parent 7c813db commit f15b8d5

File tree

2 files changed

+26
-31
lines changed

2 files changed

+26
-31
lines changed

lib/matplotlib/axis.py

+22-25
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,14 @@ def _set_labelrotation(self, labelrotation):
210210
self._labelrotation = (mode, angle)
211211

212212
def apply_tickdir(self, tickdir):
213-
"""Calculate ``self._pad`` and ``self._tickmarkers``."""
213+
"""Set tick direction. Valid values are 'out', 'in', 'inout'."""
214+
if tickdir is None:
215+
tickdir = mpl.rcParams[f'{self.__name__}.direction']
216+
_api.check_in_list(['in', 'out', 'inout'], tickdir=tickdir)
217+
self._tickdir = tickdir
218+
self._pad = self._base_pad + self.get_tick_padding()
219+
self.stale = True
220+
# Subclass overrides should compute _tickmarkers as appropriate here.
214221

215222
def get_tickdir(self):
216223
return self._tickdir
@@ -445,19 +452,13 @@ def _get_text2_transform(self):
445452
return self.axes.get_xaxis_text2_transform(self._pad)
446453

447454
def apply_tickdir(self, tickdir):
448-
"""Set tick direction. Valid values are 'in', 'out', 'inout'."""
449-
if tickdir is None:
450-
tickdir = mpl.rcParams[f'{self.__name__}.direction']
451-
_api.check_in_list(['in', 'out', 'inout'], tickdir=tickdir)
452-
self._tickdir = tickdir
453-
454-
if self._tickdir == 'in':
455-
self._tickmarkers = (mlines.TICKUP, mlines.TICKDOWN)
456-
elif self._tickdir == 'inout':
457-
self._tickmarkers = ('|', '|')
458-
else:
459-
self._tickmarkers = (mlines.TICKDOWN, mlines.TICKUP)
460-
self._pad = self._base_pad + self.get_tick_padding()
455+
# docstring inherited
456+
super().apply_tickdir(tickdir)
457+
self._tickmarkers = {
458+
'out': (mlines.TICKDOWN, mlines.TICKUP),
459+
'in': (mlines.TICKUP, mlines.TICKDOWN),
460+
'inout': ('|', '|'),
461+
}[self._tickdir]
461462
self.stale = True
462463

463464
def update_position(self, loc):
@@ -518,17 +519,13 @@ def _get_text2_transform(self):
518519
return self.axes.get_yaxis_text2_transform(self._pad)
519520

520521
def apply_tickdir(self, tickdir):
521-
if tickdir is None:
522-
tickdir = mpl.rcParams[f'{self.__name__}.direction']
523-
self._tickdir = tickdir
524-
525-
if self._tickdir == 'in':
526-
self._tickmarkers = (mlines.TICKRIGHT, mlines.TICKLEFT)
527-
elif self._tickdir == 'inout':
528-
self._tickmarkers = ('_', '_')
529-
else:
530-
self._tickmarkers = (mlines.TICKLEFT, mlines.TICKRIGHT)
531-
self._pad = self._base_pad + self.get_tick_padding()
522+
# docstring inherited
523+
super().apply_tickdir(tickdir)
524+
self._tickmarkers = {
525+
'out': (mlines.TICKLEFT, mlines.TICKRIGHT),
526+
'in': (mlines.TICKRIGHT, mlines.TICKLEFT),
527+
'inout': ('_', '_'),
528+
}[self._tickdir]
532529
self.stale = True
533530

534531
def update_position(self, loc):

lib/matplotlib/rcsetup.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -1280,15 +1280,14 @@ def _convert_validator_spec(key, conv):
12801280
"xtick.major.pad": validate_float, # distance to label in points
12811281
"xtick.minor.pad": validate_float, # distance to label in points
12821282
"xtick.color": validate_color, # color of xticks
1283-
"xtick.labelcolor": validate_color_or_inherit,
1284-
# color of xtick labels
1283+
"xtick.labelcolor": validate_color_or_inherit, # color of xtick labels
12851284
"xtick.minor.visible": validate_bool, # visibility of minor xticks
12861285
"xtick.minor.top": validate_bool, # draw top minor xticks
12871286
"xtick.minor.bottom": validate_bool, # draw bottom minor xticks
12881287
"xtick.major.top": validate_bool, # draw top major xticks
12891288
"xtick.major.bottom": validate_bool, # draw bottom major xticks
12901289
"xtick.labelsize": validate_fontsize, # fontsize of xtick labels
1291-
"xtick.direction": validate_string, # direction of xticks
1290+
"xtick.direction": ["out", "in", "inout"], # direction of xticks
12921291
"xtick.alignment": ["center", "right", "left"],
12931292

12941293
"ytick.left": validate_bool, # draw ticks on left side
@@ -1302,15 +1301,14 @@ def _convert_validator_spec(key, conv):
13021301
"ytick.major.pad": validate_float, # distance to label in points
13031302
"ytick.minor.pad": validate_float, # distance to label in points
13041303
"ytick.color": validate_color, # color of yticks
1305-
"ytick.labelcolor": validate_color_or_inherit,
1306-
# color of ytick labels
1304+
"ytick.labelcolor": validate_color_or_inherit, # color of ytick labels
13071305
"ytick.minor.visible": validate_bool, # visibility of minor yticks
13081306
"ytick.minor.left": validate_bool, # draw left minor yticks
13091307
"ytick.minor.right": validate_bool, # draw right minor yticks
13101308
"ytick.major.left": validate_bool, # draw left major yticks
13111309
"ytick.major.right": validate_bool, # draw right major yticks
13121310
"ytick.labelsize": validate_fontsize, # fontsize of ytick labels
1313-
"ytick.direction": validate_string, # direction of yticks
1311+
"ytick.direction": ["out", "in", "inout"], # direction of yticks
13141312
"ytick.alignment": [
13151313
"center", "top", "bottom", "baseline", "center_baseline"],
13161314

0 commit comments

Comments
 (0)