From f3db0af0147e1667c5127a3a7c65208957486e6a Mon Sep 17 00:00:00 2001 From: Abhinuv Nitin Pitale Date: Tue, 16 Oct 2018 17:27:50 -0700 Subject: [PATCH] Add bool support for axis(). Fixes #12311. --- lib/matplotlib/axes/_base.py | 32 ++++++++++++++++++++----------- lib/matplotlib/pyplot.py | 4 ++-- lib/matplotlib/tests/test_axes.py | 12 ++++++++++++ 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index dfda9d94479b..f994a9812d71 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -1588,7 +1588,7 @@ def apply_aspect(self, position=None): else: self.set_xbound((x0, x1)) - def axis(self, *v, **kwargs): + def axis(self, *args, **kwargs): """ Convenience method to get or set some axis properties. @@ -1605,14 +1605,15 @@ def axis(self, *v, **kwargs): The axis limits to be set. Either none or all of the limits must be given. - option : str - Possible values: + option : bool or str + If a bool, turns axis lines and labels on or off. If a string, + possible values are: ======== ========================================================== Value Description ======== ========================================================== - 'on' Turn on axis lines and labels. - 'off' Turn off axis lines and labels. + 'on' Turn on axis lines and labels. Same as ``True``. + 'off' Turn off axis lines and labels. Same as ``False``. 'equal' Set equal scaling (i.e., make circles circular) by changing axis limits. 'scaled' Set equal scaling (i.e., make circles circular) by @@ -1641,15 +1642,15 @@ def axis(self, *v, **kwargs): matplotlib.axes.Axes.set_ylim """ - if len(v) == len(kwargs) == 0: + if len(args) == len(kwargs) == 0: xmin, xmax = self.get_xlim() ymin, ymax = self.get_ylim() return xmin, xmax, ymin, ymax emit = kwargs.get('emit', True) - if len(v) == 1 and isinstance(v[0], str): - s = v[0].lower() + if len(args) == 1 and isinstance(args[0], str): + s = args[0].lower() if s == 'on': self.set_axis_on() elif s == 'off': @@ -1694,7 +1695,7 @@ def axis(self, *v, **kwargs): return xmin, xmax, ymin, ymax try: - v[0] + args[0] except IndexError: xmin = kwargs.get('xmin', None) xmax = kwargs.get('xmax', None) @@ -1711,9 +1712,18 @@ def axis(self, *v, **kwargs): ymin, ymax = self.set_ylim(ymin, ymax, emit=emit, auto=auto) return xmin, xmax, ymin, ymax - v = v[0] + v = args[0] + if isinstance(v, bool): + if v: + self.set_axis_on() + else: + self.set_axis_off() + xmin, xmax = self.get_xlim() + ymin, ymax = self.get_ylim() + return xmin, xmax, ymin, ymax + if len(v) != 4: - raise ValueError('v must contain [xmin xmax ymin ymax]') + raise ValueError('args must contain [xmin xmax ymin ymax]') self.set_xlim([v[0], v[1]], emit=emit, auto=False) self.set_ylim([v[2], v[3]], emit=emit, auto=False) diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 9f7b640e62ca..83e94feb31a8 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -2393,8 +2393,8 @@ def axhspan(ymin, ymax, xmin=0, xmax=1, **kwargs): # Autogenerated by boilerplate.py. Do not edit as changes will be lost. @docstring.copy(Axes.axis) -def axis(*v, **kwargs): - return gca().axis(*v, **kwargs) +def axis(*args, **kwargs): + return gca().axis(*args, **kwargs) # Autogenerated by boilerplate.py. Do not edit as changes will be lost. diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 8142829756b6..45384d6b30fa 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -6269,3 +6269,15 @@ def test_minor_accountedfor(): targetbb = mtransforms.Bbox.from_bounds(*targets[n]) assert_allclose(bbspines[n * 2].bounds, targetbb.bounds, atol=1e-2) + + +@check_figures_equal(extensions=["png"]) +def test_axis_bool_arguments(fig_test, fig_ref): + # Test if False and "off" give the same + fig_test.add_subplot(211).axis(False) + fig_ref.add_subplot(211).axis("off") + # Test if True after False gives the same as "on" + ax = fig_test.add_subplot(212) + ax.axis(False) + ax.axis(True) + fig_ref.add_subplot(212).axis("on")