diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 48b7a499aed6..da399a2da7d1 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -1589,7 +1589,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. @@ -1606,14 +1606,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 @@ -1642,15 +1643,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': @@ -1695,7 +1696,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) @@ -1712,9 +1713,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 3cf21457efce..20b2cb5844ed 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -6270,3 +6270,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")