Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2a05e9a

Browse files
committed
Shorten implementation of Axes methods that just wrap Axis methods.
... by autogenerating wrappers that forward to the Axis methods. Admittedly docstring search/replace is a bit ugly, but this has to be compared with having 4 copies of the docstrings for each method (Axis, xaxis, yaxis, zaxis) each to be kept in sync.
1 parent 98dff87 commit 2a05e9a

File tree

4 files changed

+92
-273
lines changed

4 files changed

+92
-273
lines changed

doc/api/next_api_changes/deprecations.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,3 +422,7 @@ Setting the *orientation* of an ``eventplot()`` or `.EventCollection` to "none"
422422
or None is deprecated; set it to "horizontal" instead. Moreover, the two
423423
orientations ("horizontal" and "vertical") will become case-sensitive in the
424424
future.
425+
426+
*minor* kwarg to `.Axis.get_ticklocs` will become keyword-only
427+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
428+
Passing this argument positionally is deprecated.

lib/matplotlib/axes/_base.py

Lines changed: 50 additions & 200 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections import OrderedDict
22
from contextlib import ExitStack
3+
import functools
34
import itertools
45
import logging
56
import math
@@ -29,6 +30,37 @@
2930
_log = logging.getLogger(__name__)
3031

3132

33+
def _axis_method_wrapper(attr_name, method_name):
34+
"""
35+
Helper to generate Axes methods wrapping Axis methods.
36+
37+
After ::
38+
39+
get_foo = _axis_method_wrapper("xaxis", "get_bar")
40+
41+
``get_foo`` is a method that forwards it arguments to the ``get_bar``
42+
method of the ``xaxis`` attribute, and gets its signature and docstring
43+
from ``Axis.get_bar``.
44+
"""
45+
46+
method = getattr(maxis.Axis, method_name)
47+
get_method = attrgetter(f"{attr_name}.{method_name}")
48+
49+
@functools.wraps(method)
50+
def wrapper(self, *args, **kwargs):
51+
return get_method(self)(*args, **kwargs)
52+
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)
60+
61+
return wrapper
62+
63+
3264
def _process_plot_format(fmt):
3365
"""
3466
Convert a MATLAB style color/line style format string to a (*linestyle*,
@@ -1761,25 +1793,14 @@ def get_xaxis(self):
17611793
"""Return the XAxis instance."""
17621794
return self.xaxis
17631795

1764-
def get_xgridlines(self):
1765-
"""Get the x grid lines as a list of `.Line2D` instances."""
1766-
return self.xaxis.get_gridlines()
1767-
1768-
def get_xticklines(self):
1769-
"""Get the x tick lines as a list of `.Line2D` instances."""
1770-
return self.xaxis.get_ticklines()
1771-
17721796
def get_yaxis(self):
17731797
"""Return the YAxis instance."""
17741798
return self.yaxis
17751799

1776-
def get_ygridlines(self):
1777-
"""Get the y grid lines as a list of `.Line2D` instances."""
1778-
return self.yaxis.get_gridlines()
1779-
1780-
def get_yticklines(self):
1781-
"""Get the y tick lines as a list of `.Line2D` instances."""
1782-
return self.yaxis.get_ticklines()
1800+
get_xgridlines = _axis_method_wrapper("xaxis", "get_gridlines")
1801+
get_xticklines = _axis_method_wrapper("xaxis", "get_ticklines")
1802+
get_ygridlines = _axis_method_wrapper("yaxis", "get_gridlines")
1803+
get_yticklines = _axis_method_wrapper("yaxis", "get_ticklines")
17831804

17841805
# Adding and tracking artists
17851806

@@ -3065,19 +3086,7 @@ def invert_xaxis(self):
30653086
"""
30663087
self.xaxis.set_inverted(not self.xaxis.get_inverted())
30673088

3068-
def xaxis_inverted(self):
3069-
"""
3070-
Return whether the x-axis is inverted.
3071-
3072-
The axis is inverted if the left value is larger than the right value.
3073-
3074-
See Also
3075-
--------
3076-
invert_xaxis
3077-
get_xlim, set_xlim
3078-
get_xbound, set_xbound
3079-
"""
3080-
return self.xaxis.get_inverted()
3089+
xaxis_inverted = _axis_method_wrapper("xaxis", "get_inverted")
30813090

30823091
def get_xbound(self):
30833092
"""
@@ -3301,15 +3310,7 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False,
33013310
self.stale = True
33023311
return left, right
33033312

3304-
def get_xscale(self):
3305-
"""
3306-
Return the x-axis scale as string.
3307-
3308-
See Also
3309-
--------
3310-
set_xscale
3311-
"""
3312-
return self.xaxis.get_scale()
3313+
get_xscale = _axis_method_wrapper("xaxis", "get_scale")
33133314

33143315
def set_xscale(self, value, **kwargs):
33153316
"""
@@ -3350,77 +3351,11 @@ def set_xscale(self, value, **kwargs):
33503351
# nonsingular() before it possibly gets swapped out by the user.
33513352
self.autoscale_view(scaley=False)
33523353

3353-
@cbook._make_keyword_only("3.2", "minor")
3354-
def get_xticks(self, minor=False):
3355-
"""Return the x ticks as a list of locations"""
3356-
return self.xaxis.get_ticklocs(minor=minor)
3357-
3358-
@cbook._make_keyword_only("3.2", "minor")
3359-
def set_xticks(self, ticks, minor=False):
3360-
"""
3361-
Set the x ticks with list of *ticks*
3362-
3363-
Parameters
3364-
----------
3365-
ticks : list
3366-
List of x-axis tick locations.
3367-
minor : bool, default: False
3368-
If ``False`` sets major ticks, if ``True`` sets minor ticks.
3369-
"""
3370-
ret = self.xaxis.set_ticks(ticks, minor=minor)
3371-
self.stale = True
3372-
return ret
3373-
3374-
def get_xmajorticklabels(self):
3375-
"""
3376-
Get the major x tick labels.
3377-
3378-
Returns
3379-
-------
3380-
list
3381-
List of `~matplotlib.text.Text` instances
3382-
"""
3383-
return self.xaxis.get_majorticklabels()
3384-
3385-
def get_xminorticklabels(self):
3386-
"""
3387-
Get the minor x tick labels.
3388-
3389-
Returns
3390-
-------
3391-
list
3392-
List of `~matplotlib.text.Text` instances
3393-
"""
3394-
return self.xaxis.get_minorticklabels()
3395-
3396-
def get_xticklabels(self, minor=False, which=None):
3397-
"""
3398-
Get the x tick labels as a list of `~matplotlib.text.Text` instances.
3399-
3400-
Parameters
3401-
----------
3402-
minor : bool, optional
3403-
If True return the minor ticklabels,
3404-
else return the major ticklabels.
3405-
3406-
which : None, ('minor', 'major', 'both')
3407-
Overrides *minor*.
3408-
3409-
Selects which ticklabels to return
3410-
3411-
Returns
3412-
-------
3413-
list
3414-
List of `~matplotlib.text.Text` instances.
3415-
3416-
Notes
3417-
-----
3418-
The tick label strings are not populated until a ``draw``
3419-
method has been called.
3420-
3421-
See also: `~.pyplot.draw` and `~.FigureCanvasBase.draw`.
3422-
"""
3423-
return self.xaxis.get_ticklabels(minor=minor, which=which)
3354+
get_xticks = _axis_method_wrapper("xaxis", "get_ticklocs")
3355+
set_xticks = _axis_method_wrapper("xaxis", "set_ticks")
3356+
get_xmajorticklabels = _axis_method_wrapper("xaxis", "get_majorticklabels")
3357+
get_xminorticklabels = _axis_method_wrapper("xaxis", "get_minorticklabels")
3358+
get_xticklabels = _axis_method_wrapper("xaxis", "get_ticklabels")
34243359

34253360
@cbook._make_keyword_only("3.3", "fontdict")
34263361
def set_xticklabels(self, labels, fontdict=None, minor=False, **kwargs):
@@ -3477,19 +3412,7 @@ def invert_yaxis(self):
34773412
"""
34783413
self.yaxis.set_inverted(not self.yaxis.get_inverted())
34793414

3480-
def yaxis_inverted(self):
3481-
"""
3482-
Return whether the y-axis is inverted.
3483-
3484-
The axis is inverted if the bottom value is larger than the top value.
3485-
3486-
See Also
3487-
--------
3488-
invert_yaxis
3489-
get_ylim, set_ylim
3490-
get_ybound, set_ybound
3491-
"""
3492-
return self.yaxis.get_inverted()
3415+
yaxis_inverted = _axis_method_wrapper("yaxis", "get_inverted")
34933416

34943417
def get_ybound(self):
34953418
"""
@@ -3696,15 +3619,7 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
36963619
self.stale = True
36973620
return bottom, top
36983621

3699-
def get_yscale(self):
3700-
"""
3701-
Return the y-axis scale as string.
3702-
3703-
See Also
3704-
--------
3705-
set_yscale
3706-
"""
3707-
return self.yaxis.get_scale()
3622+
get_yscale = _axis_method_wrapper("yaxis", "get_scale")
37083623

37093624
def set_yscale(self, value, **kwargs):
37103625
"""
@@ -3745,76 +3660,11 @@ def set_yscale(self, value, **kwargs):
37453660
# nonsingular() before it possibly gets swapped out by the user.
37463661
self.autoscale_view(scalex=False)
37473662

3748-
@cbook._make_keyword_only("3.2", "minor")
3749-
def get_yticks(self, minor=False):
3750-
"""Return the y ticks as a list of locations"""
3751-
return self.yaxis.get_ticklocs(minor=minor)
3752-
3753-
@cbook._make_keyword_only("3.2", "minor")
3754-
def set_yticks(self, ticks, minor=False):
3755-
"""
3756-
Set the y ticks with list of *ticks*
3757-
3758-
Parameters
3759-
----------
3760-
ticks : list
3761-
List of y-axis tick locations
3762-
minor : bool, default: False
3763-
If ``False`` sets major ticks, if ``True`` sets minor ticks.
3764-
"""
3765-
ret = self.yaxis.set_ticks(ticks, minor=minor)
3766-
return ret
3767-
3768-
def get_ymajorticklabels(self):
3769-
"""
3770-
Get the major y tick labels.
3771-
3772-
Returns
3773-
-------
3774-
list
3775-
List of `~matplotlib.text.Text` instances
3776-
"""
3777-
return self.yaxis.get_majorticklabels()
3778-
3779-
def get_yminorticklabels(self):
3780-
"""
3781-
Get the minor y tick labels.
3782-
3783-
Returns
3784-
-------
3785-
list
3786-
List of `~matplotlib.text.Text` instances
3787-
"""
3788-
return self.yaxis.get_minorticklabels()
3789-
3790-
def get_yticklabels(self, minor=False, which=None):
3791-
"""
3792-
Get the y tick labels as a list of `~matplotlib.text.Text` instances.
3793-
3794-
Parameters
3795-
----------
3796-
minor : bool
3797-
If True return the minor ticklabels,
3798-
else return the major ticklabels
3799-
3800-
which : None, ('minor', 'major', 'both')
3801-
Overrides *minor*.
3802-
3803-
Selects which ticklabels to return
3804-
3805-
Returns
3806-
-------
3807-
list
3808-
List of `~matplotlib.text.Text` instances.
3809-
3810-
Notes
3811-
-----
3812-
The tick label strings are not populated until a ``draw``
3813-
method has been called.
3814-
3815-
See also: `~.pyplot.draw` and `~.FigureCanvasBase.draw`.
3816-
"""
3817-
return self.yaxis.get_ticklabels(minor=minor, which=which)
3663+
get_yticks = _axis_method_wrapper("yaxis", "get_ticklocs")
3664+
set_yticks = _axis_method_wrapper("yaxis", "set_ticks")
3665+
get_ymajorticklabels = _axis_method_wrapper("yaxis", "get_majorticklabels")
3666+
get_yminorticklabels = _axis_method_wrapper("yaxis", "get_minorticklabels")
3667+
get_yticklabels = _axis_method_wrapper("yaxis", "get_ticklabels")
38183668

38193669
@cbook._make_keyword_only("3.3", "fontdict")
38203670
def set_yticklabels(self, labels, fontdict=None, minor=False, **kwargs):

0 commit comments

Comments
 (0)