|
1 | 1 | from collections import OrderedDict |
2 | 2 | from contextlib import ExitStack |
| 3 | +import functools |
3 | 4 | import itertools |
4 | 5 | import logging |
5 | 6 | import math |
|
29 | 30 | _log = logging.getLogger(__name__) |
30 | 31 |
|
31 | 32 |
|
| 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 | + |
32 | 64 | def _process_plot_format(fmt): |
33 | 65 | """ |
34 | 66 | Convert a MATLAB style color/line style format string to a (*linestyle*, |
@@ -1761,25 +1793,14 @@ def get_xaxis(self): |
1761 | 1793 | """Return the XAxis instance.""" |
1762 | 1794 | return self.xaxis |
1763 | 1795 |
|
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 | | - |
1772 | 1796 | def get_yaxis(self): |
1773 | 1797 | """Return the YAxis instance.""" |
1774 | 1798 | return self.yaxis |
1775 | 1799 |
|
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") |
1783 | 1804 |
|
1784 | 1805 | # Adding and tracking artists |
1785 | 1806 |
|
@@ -3065,19 +3086,7 @@ def invert_xaxis(self): |
3065 | 3086 | """ |
3066 | 3087 | self.xaxis.set_inverted(not self.xaxis.get_inverted()) |
3067 | 3088 |
|
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") |
3081 | 3090 |
|
3082 | 3091 | def get_xbound(self): |
3083 | 3092 | """ |
@@ -3301,15 +3310,7 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, |
3301 | 3310 | self.stale = True |
3302 | 3311 | return left, right |
3303 | 3312 |
|
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") |
3313 | 3314 |
|
3314 | 3315 | def set_xscale(self, value, **kwargs): |
3315 | 3316 | """ |
@@ -3350,77 +3351,11 @@ def set_xscale(self, value, **kwargs): |
3350 | 3351 | # nonsingular() before it possibly gets swapped out by the user. |
3351 | 3352 | self.autoscale_view(scaley=False) |
3352 | 3353 |
|
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") |
3424 | 3359 |
|
3425 | 3360 | @cbook._make_keyword_only("3.3", "fontdict") |
3426 | 3361 | def set_xticklabels(self, labels, fontdict=None, minor=False, **kwargs): |
@@ -3477,19 +3412,7 @@ def invert_yaxis(self): |
3477 | 3412 | """ |
3478 | 3413 | self.yaxis.set_inverted(not self.yaxis.get_inverted()) |
3479 | 3414 |
|
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") |
3493 | 3416 |
|
3494 | 3417 | def get_ybound(self): |
3495 | 3418 | """ |
@@ -3696,15 +3619,7 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, |
3696 | 3619 | self.stale = True |
3697 | 3620 | return bottom, top |
3698 | 3621 |
|
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") |
3708 | 3623 |
|
3709 | 3624 | def set_yscale(self, value, **kwargs): |
3710 | 3625 | """ |
@@ -3745,76 +3660,11 @@ def set_yscale(self, value, **kwargs): |
3745 | 3660 | # nonsingular() before it possibly gets swapped out by the user. |
3746 | 3661 | self.autoscale_view(scalex=False) |
3747 | 3662 |
|
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") |
3818 | 3668 |
|
3819 | 3669 | @cbook._make_keyword_only("3.3", "fontdict") |
3820 | 3670 | def set_yticklabels(self, labels, fontdict=None, minor=False, **kwargs): |
|
0 commit comments