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

Skip to content

Commit 7b10000

Browse files
authored
Merge pull request #13670 from anntzer/axis
Simplify the logic of axis().
2 parents e2bec3f + 86dd271 commit 7b10000

File tree

3 files changed

+46
-53
lines changed

3 files changed

+46
-53
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Deprecations
2+
````````````
3+
4+
Passing more than one positional argument or unsupported keyword arguments to
5+
`~matplotlib.axes.Axes.axis()` is deprecated (such arguments used to be
6+
silently ignored).

lib/matplotlib/axes/_base.py

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,7 @@ def apply_aspect(self, position=None):
16101610
else:
16111611
self.set_xbound((x0, x1))
16121612

1613-
def axis(self, *args, **kwargs):
1613+
def axis(self, *args, emit=True, **kwargs):
16141614
"""
16151615
Convenience method to get or set some axis properties.
16161616
@@ -1624,8 +1624,7 @@ def axis(self, *args, **kwargs):
16241624
Parameters
16251625
----------
16261626
xmin, xmax, ymin, ymax : float, optional
1627-
The axis limits to be set. Either none or all of the limits must
1628-
be given. This can also be achieved using ::
1627+
The axis limits to be set. This can also be achieved using ::
16291628
16301629
ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax))
16311630
@@ -1665,16 +1664,13 @@ def axis(self, *args, **kwargs):
16651664
matplotlib.axes.Axes.set_xlim
16661665
matplotlib.axes.Axes.set_ylim
16671666
"""
1668-
1669-
if len(args) == len(kwargs) == 0:
1670-
xmin, xmax = self.get_xlim()
1671-
ymin, ymax = self.get_ylim()
1672-
return xmin, xmax, ymin, ymax
1673-
1674-
emit = kwargs.get('emit', True)
1675-
1676-
if len(args) == 1 and isinstance(args[0], str):
1677-
s = args[0].lower()
1667+
if len(args) == 1 and isinstance(args[0], (str, bool)):
1668+
s = args[0]
1669+
if s is True:
1670+
s = 'on'
1671+
if s is False:
1672+
s = 'off'
1673+
s = s.lower()
16781674
if s == 'on':
16791675
self.set_axis_on()
16801676
elif s == 'off':
@@ -1714,45 +1710,36 @@ def axis(self, *args, **kwargs):
17141710
else:
17151711
raise ValueError('Unrecognized string %s to axis; '
17161712
'try on or off' % s)
1717-
xmin, xmax = self.get_xlim()
1718-
ymin, ymax = self.get_ylim()
1719-
return xmin, xmax, ymin, ymax
1720-
1721-
try:
1722-
args[0]
1723-
except IndexError:
1724-
xmin = kwargs.get('xmin', None)
1725-
xmax = kwargs.get('xmax', None)
1726-
auto = False # turn off autoscaling, unless...
1727-
if xmin is None and xmax is None:
1728-
auto = None # leave autoscaling state alone
1729-
xmin, xmax = self.set_xlim(xmin, xmax, emit=emit, auto=auto)
1730-
1731-
ymin = kwargs.get('ymin', None)
1732-
ymax = kwargs.get('ymax', None)
1733-
auto = False # turn off autoscaling, unless...
1734-
if ymin is None and ymax is None:
1735-
auto = None # leave autoscaling state alone
1736-
ymin, ymax = self.set_ylim(ymin, ymax, emit=emit, auto=auto)
1737-
return xmin, xmax, ymin, ymax
1738-
1739-
v = args[0]
1740-
if isinstance(v, bool):
1741-
if v:
1742-
self.set_axis_on()
1713+
else:
1714+
if len(args) >= 1:
1715+
if len(args) != 1:
1716+
cbook.warn_deprecated(
1717+
"3.2", message="Passing more than one positional "
1718+
"argument to axis() is deprecated and will raise a "
1719+
"TypeError %(removal)s.")
1720+
v = args[0]
1721+
try:
1722+
xmin, xmax, ymin, ymax = v
1723+
except ValueError:
1724+
raise ValueError('args must contain [xmin xmax ymin ymax]')
17431725
else:
1744-
self.set_axis_off()
1745-
xmin, xmax = self.get_xlim()
1746-
ymin, ymax = self.get_ylim()
1747-
return xmin, xmax, ymin, ymax
1748-
1749-
if len(v) != 4:
1750-
raise ValueError('args must contain [xmin xmax ymin ymax]')
1751-
1752-
self.set_xlim([v[0], v[1]], emit=emit, auto=False)
1753-
self.set_ylim([v[2], v[3]], emit=emit, auto=False)
1754-
1755-
return v
1726+
xmin = kwargs.pop('xmin', None)
1727+
xmax = kwargs.pop('xmax', None)
1728+
ymin = kwargs.pop('ymin', None)
1729+
ymax = kwargs.pop('ymax', None)
1730+
xauto = (None # Keep autoscale state as is.
1731+
if xmin is None and xmax is None
1732+
else False) # Turn off autoscale.
1733+
yauto = (None
1734+
if ymin is None and ymax is None
1735+
else False)
1736+
self.set_xlim(xmin, xmax, emit=emit, auto=xauto)
1737+
self.set_ylim(ymin, ymax, emit=emit, auto=yauto)
1738+
if kwargs:
1739+
cbook.warn_deprecated(
1740+
"3.1", message="Passing unsupported keyword arguments to "
1741+
"axis() will raise a TypeError %(removal)s.")
1742+
return (*self.get_xlim(), *self.get_ylim())
17561743

17571744
def get_legend(self):
17581745
"""Return the `Legend` instance, or None if no legend is defined."""

lib/matplotlib/pyplot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,8 +2418,8 @@ def axhspan(ymin, ymax, xmin=0, xmax=1, **kwargs):
24182418

24192419
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
24202420
@docstring.copy(Axes.axis)
2421-
def axis(*args, **kwargs):
2422-
return gca().axis(*args, **kwargs)
2421+
def axis(*args, emit=True, **kwargs):
2422+
return gca().axis(*args, emit=emit, **kwargs)
24232423

24242424

24252425
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.

0 commit comments

Comments
 (0)