Skip to content

Commit 07abbbc

Browse files
committed
Also templatize set_x/y/zticklabels.
1 parent 1a9252e commit 07abbbc

File tree

3 files changed

+65
-101
lines changed

3 files changed

+65
-101
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 20 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
_log = logging.getLogger(__name__)
3131

3232

33-
def _axis_method_wrapper(attr_name, method_name):
33+
def _axis_method_wrapper(attr_name, method_name, *, doc_sub=None):
3434
"""
3535
Helper to generate Axes methods wrapping Axis methods.
3636
@@ -41,6 +41,10 @@ def _axis_method_wrapper(attr_name, method_name):
4141
``get_foo`` is a method that forwards it arguments to the ``get_bar``
4242
method of the ``xaxis`` attribute, and gets its signature and docstring
4343
from ``Axis.get_bar``.
44+
45+
The docstring of ``get_foo`` is built by replacing "this Axis" by "the
46+
{attr_name}" ("the xaxis", "the yaxis") in the wrapped method's docstring;
47+
additional replacements can by given in *doc_sub*.
4448
"""
4549

4650
method = getattr(maxis.Axis, method_name)
@@ -50,13 +54,15 @@ def _axis_method_wrapper(attr_name, method_name):
5054
def wrapper(self, *args, **kwargs):
5155
return get_method(self)(*args, **kwargs)
5256

53-
if wrapper.__doc__:
54-
assert "this Axis" in wrapper.__doc__, \
55-
(f"The docstring of wrapped Axis methods must contain "
56-
f"'this Axis' as a substring, but this is not the case for "
57-
f"{method_name}")
58-
wrapper.__doc__ = wrapper.__doc__.replace(
59-
"this Axis", f"the {attr_name}", 1)
57+
doc = wrapper.__doc__
58+
if doc:
59+
doc_sub = {"this Axis": f"the {attr_name}", **(doc_sub or {})}
60+
for k, v in doc_sub.items():
61+
assert k in doc, \
62+
(f"The docstring of wrapped Axis method {method_name!r} must "
63+
f"contain {k!r} as a substring.")
64+
doc = doc.replace(k, v)
65+
wrapper.__doc__ = doc
6066

6167
return wrapper
6268

@@ -3356,49 +3362,9 @@ def set_xscale(self, value, **kwargs):
33563362
get_xmajorticklabels = _axis_method_wrapper("xaxis", "get_majorticklabels")
33573363
get_xminorticklabels = _axis_method_wrapper("xaxis", "get_minorticklabels")
33583364
get_xticklabels = _axis_method_wrapper("xaxis", "get_ticklabels")
3359-
3360-
@cbook._make_keyword_only("3.3", "fontdict")
3361-
def set_xticklabels(self, labels, fontdict=None, minor=False, **kwargs):
3362-
"""
3363-
Set the x-tick labels with list of string labels.
3364-
3365-
.. warning::
3366-
This method should only be used after fixing the tick positions
3367-
using `~.axes.Axes.set_xticks`. Otherwise, the labels may end up
3368-
in unexpected positions.
3369-
3370-
Parameters
3371-
----------
3372-
labels : list of str
3373-
The label texts.
3374-
3375-
fontdict : dict, optional
3376-
A dictionary controlling the appearance of the ticklabels.
3377-
The default *fontdict* is::
3378-
3379-
{'fontsize': rcParams['axes.titlesize'],
3380-
'fontweight': rcParams['axes.titleweight'],
3381-
'verticalalignment': 'baseline',
3382-
'horizontalalignment': loc}
3383-
3384-
minor : bool, default: False
3385-
Whether to set the minor ticklabels rather than the major ones.
3386-
3387-
Returns
3388-
-------
3389-
list of `~.Text`
3390-
The labels.
3391-
3392-
Other Parameters
3393-
----------------
3394-
**kwargs : `~.text.Text` properties.
3395-
"""
3396-
if fontdict is not None:
3397-
kwargs.update(fontdict)
3398-
ret = self.xaxis.set_ticklabels(labels,
3399-
minor=minor, **kwargs)
3400-
self.stale = True
3401-
return ret
3365+
set_xticklabels = _axis_method_wrapper(
3366+
"xaxis", "_set_ticklabels",
3367+
doc_sub={"Axis.set_ticks": "Axes.set_xticks"})
34023368

34033369
def invert_yaxis(self):
34043370
"""
@@ -3665,47 +3631,9 @@ def set_yscale(self, value, **kwargs):
36653631
get_ymajorticklabels = _axis_method_wrapper("yaxis", "get_majorticklabels")
36663632
get_yminorticklabels = _axis_method_wrapper("yaxis", "get_minorticklabels")
36673633
get_yticklabels = _axis_method_wrapper("yaxis", "get_ticklabels")
3668-
3669-
@cbook._make_keyword_only("3.3", "fontdict")
3670-
def set_yticklabels(self, labels, fontdict=None, minor=False, **kwargs):
3671-
"""
3672-
Set the y-tick labels with list of string labels.
3673-
3674-
.. warning::
3675-
This method should only be used after fixing the tick positions
3676-
using `~.axes.Axes.set_yticks`. Otherwise, the labels may end up
3677-
in unexpected positions.
3678-
3679-
Parameters
3680-
----------
3681-
labels : list of str
3682-
The label texts.
3683-
3684-
fontdict : dict, optional
3685-
A dictionary controlling the appearance of the ticklabels.
3686-
The default *fontdict* is::
3687-
3688-
{'fontsize': rcParams['axes.titlesize'],
3689-
'fontweight': rcParams['axes.titleweight'],
3690-
'verticalalignment': 'baseline',
3691-
'horizontalalignment': loc}
3692-
3693-
minor : bool, default: False
3694-
Whether to set the minor ticklabels rather than the major ones.
3695-
3696-
Returns
3697-
-------
3698-
labels
3699-
A list of `~.text.Text` instances.
3700-
3701-
Other Parameters
3702-
----------------
3703-
**kwargs : `~.text.Text` properties.
3704-
"""
3705-
if fontdict is not None:
3706-
kwargs.update(fontdict)
3707-
return self.yaxis.set_ticklabels(labels,
3708-
minor=minor, **kwargs)
3634+
set_yticklabels = _axis_method_wrapper(
3635+
"yaxis", "_set_ticklabels",
3636+
doc_sub={"Axis.set_ticks": "Axes.set_yticks"})
37093637

37103638
def xaxis_date(self, tz=None):
37113639
"""

lib/matplotlib/axis.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,48 @@ def set_ticklabels(self, ticklabels, *, minor=False, **kwargs):
16471647
self.stale = True
16481648
return ret
16491649

1650+
# Wrapper around set_ticklabels used to generate Axes.set_x/ytickabels; can
1651+
# go away once the API of Axes.set_x/yticklabels becomes consistent.
1652+
@cbook._make_keyword_only("3.3", "fontdict")
1653+
def _set_ticklabels(self, labels, fontdict=None, minor=False, **kwargs):
1654+
"""
1655+
Set this Axis' labels with list of string labels.
1656+
1657+
.. warning::
1658+
This method should only be used after fixing the tick positions
1659+
using `.Axis.set_ticks`. Otherwise, the labels may end up in
1660+
unexpected positions.
1661+
1662+
Parameters
1663+
----------
1664+
labels : list of str
1665+
The label texts.
1666+
1667+
fontdict : dict, optional
1668+
A dictionary controlling the appearance of the ticklabels.
1669+
The default *fontdict* is::
1670+
1671+
{'fontsize': rcParams['axes.titlesize'],
1672+
'fontweight': rcParams['axes.titleweight'],
1673+
'verticalalignment': 'baseline',
1674+
'horizontalalignment': loc}
1675+
1676+
minor : bool, default: False
1677+
Whether to set the minor ticklabels rather than the major ones.
1678+
1679+
Returns
1680+
-------
1681+
list of `~.Text`
1682+
The labels.
1683+
1684+
Other Parameters
1685+
----------------
1686+
**kwargs : `~.text.Text` properties.
1687+
"""
1688+
if fontdict is not None:
1689+
kwargs.update(fontdict)
1690+
return self.set_ticklabels(labels, minor=minor, **kwargs)
1691+
16501692
@cbook._make_keyword_only("3.2", "minor")
16511693
def set_ticks(self, ticks, minor=False):
16521694
"""

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -823,15 +823,9 @@ def set_zscale(self, value, **kwargs):
823823
get_zmajorticklabels = _axis_method_wrapper("zaxis", "get_majorticklabels")
824824
get_zminorticklabels = _axis_method_wrapper("zaxis", "get_minorticklabels")
825825
get_zticklabels = _axis_method_wrapper("zaxis", "get_ticklabels")
826-
827-
def set_zticklabels(self, *args, **kwargs):
828-
"""
829-
Set z-axis tick labels.
830-
See :meth:`matplotlib.axes.Axes.set_yticklabels` for more details.
831-
832-
.. versionadded:: 1.1.0
833-
"""
834-
return self.zaxis.set_ticklabels(*args, **kwargs)
826+
set_zticklabels = _axis_method_wrapper(
827+
"zaxis", "_set_ticklabels",
828+
doc_sub={"Axis.set_ticks": "Axes.set_zticks"})
835829

836830
def zaxis_date(self, tz=None):
837831
"""

0 commit comments

Comments
 (0)