Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5350e8a

Browse files
committed
Also templatize set_x/y/zticklabels.
1 parent2a05e9a commit5350e8a

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
defwrapper(self,*args,**kwargs):
5155
returnget_method(self)(*args,**kwargs)
5256

53-
ifwrapper.__doc__:
54-
assert"this Axis"inwrapper.__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+
ifdoc:
59+
doc_sub= {"this Axis":f"the{attr_name}",**(doc_subor {})}
60+
fork,vindoc_sub.items():
61+
assertkindoc, \
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
returnwrapper
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-
defset_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-
iffontdictisnotNone:
3397-
kwargs.update(fontdict)
3398-
ret=self.xaxis.set_ticklabels(labels,
3399-
minor=minor,**kwargs)
3400-
self.stale=True
3401-
returnret
3365+
set_xticklabels=_axis_method_wrapper(
3366+
"xaxis","_set_ticklabels",
3367+
doc_sub={"Axis.set_ticks":"Axes.set_xticks"})
34023368

34033369
definvert_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-
defset_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-
iffontdictisnotNone:
3706-
kwargs.update(fontdict)
3707-
returnself.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
defxaxis_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
returnret
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+
iffontdictisnotNone:
1689+
kwargs.update(fontdict)
1690+
returnself.set_ticklabels(labels,minor=minor,**kwargs)
1691+
16501692
@cbook._make_keyword_only("3.2","minor")
16511693
defset_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
@@ -826,15 +826,9 @@ def set_zscale(self, value, **kwargs):
826826
get_zmajorticklabels=_axis_method_wrapper("zaxis","get_majorticklabels")
827827
get_zminorticklabels=_axis_method_wrapper("zaxis","get_minorticklabels")
828828
get_zticklabels=_axis_method_wrapper("zaxis","get_ticklabels")
829-
830-
defset_zticklabels(self,*args,**kwargs):
831-
"""
832-
Set z-axis tick labels.
833-
See :meth:`matplotlib.axes.Axes.set_yticklabels` for more details.
834-
835-
.. versionadded:: 1.1.0
836-
"""
837-
returnself.zaxis.set_ticklabels(*args,**kwargs)
829+
set_zticklabels=_axis_method_wrapper(
830+
"zaxis","_set_ticklabels",
831+
doc_sub={"Axis.set_ticks":"Axes.set_zticks"})
838832

839833
defzaxis_date(self,tz=None):
840834
"""

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp