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 """
0 commit comments