8000 Merge pull request #16473 from eric-wieser/generalize-xy · matplotlib/matplotlib@811c2a4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 811c2a4

Browse files
authored
Merge pull request #16473 from eric-wieser/generalize-xy
Make `.axis(zmin=...)` work on 3D axes
2 parents def68de + 0580d94 commit 811c2a4

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,6 +2054,11 @@ def axis(self, arg=None, /, *, emit=True, **kwargs):
20542054
--------
20552055
matplotlib.axes.Axes.set_xlim
20562056
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.
20572062
"""
20582063
if isinstance(arg, (str, bool)):
20592064
if arg is True:
@@ -2097,28 +2102,34 @@ def axis(self, arg=None, /, *, emit=True, **kwargs):
20972102
"try 'on' or 'off'")
20982103
else:
20992104
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+
}
21062114
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)
21192126
if kwargs:
21202127
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
21222133

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

lib/matplotlib/tests/test_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5641,7 +5641,7 @@ def test_shared_aspect_error():
56415641
"Unrecognized string 'foo' to axis; try 'on' or "
56425642
"'off'"),
56435643
(TypeError, ([1, 2], ), {},
5644-
"the first argument to axis*"),
5644+
"The first argument to axis*"),
56455645
(TypeError, tuple(), {'foo': None},
56465646
r"axis\(\) got an unexpected keyword argument "
56475647
"'foo'"),

lib/mpl_toolkits/mplot3d/tests/test_axes3d.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,7 @@ def test_contour3d():
213213
ax.contour(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm)
214214
ax.contour(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm)
215215
ax.contour(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm)
216-
ax.set_xlim(-40, 40)
217-
ax.set_ylim(-40, 40)
218-
ax.set_zlim(-100, 100)
216+
ax.axis(xmin=-40, xmax=40, ymin=-40, ymax=40, zmin=-100, zmax=100)
219217

220218

221219
@mpl3d_image_comparison(['contour3d_extend3d.png'], style='mpl20')

0 commit comments

Comments
 (0)
0