@@ -2054,6 +2054,11 @@ def axis(self, arg=None, /, *, emit=True, **kwargs):
2054
2054
--------
2055
2055
matplotlib.axes.Axes.set_xlim
2056
2056
matplotlib.axes.Axes.set_ylim
2057
+
2058
+ Notes
2059
+ -----
2060
+ For 3D axes, this method additionally takes *zmin*, *zmax* as
2061
+ parameters and likewise returns them.
2057
2062
"""
2058
2063
if isinstance (arg , (str , bool )):
2059
2064
if arg is True :
@@ -2097,28 +2102,34 @@ def axis(self, arg=None, /, *, emit=True, **kwargs):
2097
2102
"try 'on' or 'off'" )
2098
2103
else :
2099
2104
if arg is not None :
2100
- try :
2101
- xmin , xmax , ymin , ymax = arg
2102
- except (TypeError , ValueError ) as err :
2103
- raise TypeError ('the first argument to axis() must be an '
2104
- 'iterable of the form '
2105
- '[xmin, xmax, ymin, ymax]' ) from err
2105
+ if len (arg ) != 2 * len (self ._axis_names ):
2106
+ raise TypeError (
2107
+ "The first argument to axis() must be an iterable of the form "
2108
+ "[{}]" .format (", " .join (
2109
+ f"{ name } min, { name } max" for name in self ._axis_names )))
2110
+ limits = {
2111
+ name : arg [2 * i :2 * (i + 1 )]
2112
+ for i , name in enumerate (self ._axis_names )
2113
+ }
2106
2114
else :
2107
- xmin = kwargs .pop ('xmin' , None )
2108
- xmax = kwargs .pop ('xmax' , None )
2109
- ymin = kwargs .pop ('ymin' , None )
2110
- ymax = kwargs .pop ('ymax' , None )
2111
- xauto = (None # Keep autoscale state as is.
2112
- if xmin is None and xmax is None
2113
- else False ) # Turn off autoscale.
2114
- yauto = (None
2115
- if ymin is None and ymax is None
2116
- else False )
2117
- self .set_xlim (xmin , xmax , emit = emit , auto = xauto )
2118
- self .set_ylim (ymin , ymax , emit = emit , auto = yauto )
2115
+ limits = {}
2116
+ for name in self ._axis_names :
2117
+ ax_min = kwargs .pop (f'{ name } min' , None )
2118
+ ax_max = kwargs .pop (f'{ name } max' , None )
2119
+ limits [name ] = (ax_min , ax_max )
2120
+ for name , (ax_min , ax_max ) in limits .items ():
2121
+ ax_auto = (None # Keep autoscale state as is.
2122
+ if ax_min is None and ax_max is None
2123
+ else False ) # Turn off autoscale.
2124
+ set_ax_lim = getattr (self , f'set_{ name } lim' )
2125
+ set_ax_lim (ax_min , ax_max , emit = emit , auto = ax_auto )
2119
2126
if kwargs :
2120
2127
raise _api .kwarg_error ("axis" , kwargs )
2121
- return (* self .get_xlim (), * self .get_ylim ())
2128
+ lims = ()
2129
+ for name in self ._axis_names :
2130
+ get_ax_lim = getattr (self , f'get_{ name } lim' )
2131
+ lims += get_ax_lim ()
2132
+ return lims
2122
2133
2123
2134
def get_legend (self ):
2124
2135
"""Return the `.Legend` instance, or None if no legend is defined."""
0 commit comments