Skip to content

Commit 120328f

Browse files
committed
Simplify tick creation
The tick class hierarchy has a standard mechanism for creating ticks via `_get_tick()` The only element that is changing in these functions is the tick class. All other logic is identical. This PR makes the tick class a class attribute of Axis so that we can use one generic `_get_tick` implementation across all Axis subclasses. Note that users/downstream libraries can still define their own `_get_tick()` methods on subclasses if they want to have a different behavior.
1 parent 4e20f15 commit 120328f

File tree

2 files changed

+13
-29
lines changed

2 files changed

+13
-29
lines changed

lib/matplotlib/axis.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,9 @@ class Axis(martist.Artist):
635635
The minor ticks.
636636
"""
637637
OFFSETTEXTPAD = 3
638+
# The class used in _get_tick() to create tick instances. Must either be
639+
# overwritten in subclasses, or subclasses must reimplement _get_tick().
640+
_tick_class = None
638641

639642
def __str__(self):
640643
return "{}({},{})".format(
@@ -1437,7 +1440,12 @@ def get_ticks_direction(self, minor=False):
14371440

14381441
def _get_tick(self, major):
14391442
"""Return the default tick instance."""
1440-
raise NotImplementedError('derived must override')
1443+
if self._tick_class is None:
1444+
raise NotImplementedError(
1445+
f"The Axis subclass {self.__class__.__name__} must define "
1446+
"_tick_class or reimplement _get_tick()")
1447+
tick_kw = self._major_tick_kw if major else self._minor_tick_kw
1448+
return self._tick_class(self.axes, 0, major=major, **tick_kw)
14411449

14421450
def _get_tick_label_size(self, axis_name):
14431451
"""
@@ -2133,6 +2141,7 @@ def setter(self, vmin, vmax, ignore=False):
21332141
class XAxis(Axis):
21342142
__name__ = 'xaxis'
21352143
axis_name = 'x' #: Read-only name identifying the axis.
2144+
_tick_class = XTick
21362145

21372146
def __init__(self, *args, **kwargs):
21382147
super().__init__(*args, **kwargs)
@@ -2173,13 +2182,6 @@ def contains(self, mouseevent):
21732182
t < y < t + self.pickradius)
21742183
return inaxis, {}
21752184

2176-
def _get_tick(self, major):
2177-
if major:
2178-
tick_kw = self._major_tick_kw
2179-
else:
2180-
tick_kw = self._minor_tick_kw
2181-
return XTick(self.axes, 0, major=major, **tick_kw)
2182-
21832185
def set_label_position(self, position):
21842186
"""
21852187
Set the label position (top or bottom)
@@ -2389,6 +2391,7 @@ def get_tick_space(self):
23892391
class YAxis(Axis):
23902392
__name__ = 'yaxis'
23912393
axis_name = 'y' #: Read-only name identifying the axis.
2394+
_tick_class = YTick
23922395

23932396
def __init__(self, *args, **kwargs):
23942397
super().__init__(*args, **kwargs)
@@ -2431,13 +2434,6 @@ def contains(self, mouseevent):
24312434
r < x < r + self.pickradius)
24322435
return inaxis, {}
24332436

2434-
def _get_tick(self, major):
2435-
if major:
2436-
tick_kw = self._major_tick_kw
2437-
else:
2438-
tick_kw = self._minor_tick_kw
2439-
return YTick(self.axes, 0, major=major, **tick_kw)
2440-
24412437
def set_label_position(self, position):
24422438
"""
24432439
Set the label position (left or right)

lib/matplotlib/projections/polar.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,7 @@ class ThetaAxis(maxis.XAxis):
358358
"""
359359
__name__ = 'thetaaxis'
360360
axis_name = 'theta' #: Read-only name identifying the axis.
361-
362-
def _get_tick(self, major):
363-
if major:
364-
tick_kw = self._major_tick_kw
365-
else:
366-
tick_kw = self._minor_tick_kw
367-
return ThetaTick(self.axes, 0, major=major, **tick_kw)
361+
_tick_class = ThetaTick
368362

369363
def _wrap_locator_formatter(self):
370364
self.set_major_locator(ThetaLocator(self.get_major_locator()))
@@ -653,18 +647,12 @@ class RadialAxis(maxis.YAxis):
653647
"""
654648
__name__ = 'radialaxis'
655649
axis_name = 'radius' #: Read-only name identifying the axis.
650+
_tick_class = RadialTick
656651

657652
def __init__(self, *args, **kwargs):
658653
super().__init__(*args, **kwargs)
659654
self.sticky_edges.y.append(0)
660655

661-
def _get_tick(self, major):
662-
if major:
663-
tick_kw = self._major_tick_kw
664-
else:
665-
tick_kw = self._minor_tick_kw
666-
return RadialTick(self.axes, 0, major=major, **tick_kw)
667-
668656
def _wrap_locator_formatter(self):
669657
self.set_major_locator(RadialLocator(self.get_major_locator(),
670658
self.axes))

0 commit comments

Comments
 (0)