8000 Make `.axis(zmin=...)` work on 3D axes · matplotlib/matplotlib@2209b9f · GitHub
[go: up one dir, main page]

Skip to content

Commit 2209b9f

Browse files
eric-wieseranntzer
authored andcommitted
Make .axis(zmin=...) work on 3D axes
This adds an `_axis_names` member to make it possible to generalize parts of `_AxesBase` to work in 3D too.
1 parent a861b8a commit 2209b9f

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,28 +2099,34 @@ def axis(self, arg=None, /, *, emit=True, **kwargs):
20992099
"try 'on' or 'off'")
21002100
else:
21012101
if arg is not None:
2102-
try:
2103-
xmin, xmax, ymin, ymax = arg
2104-
except (TypeError, ValueError) as err:
2105-
raise TypeError('the first argument to axis() must be an '
2106-
'iterable of the form '
2107-
'[xmin, xmax, ymin, ymax]') from err
2102+
if len(arg) != 2*len(self._axis_names):
2103+
raise TypeError(
2104+
"The first argument to axis() must be an iterable of the form "
2105+
"[{}]".format(", ".join(
2106+
f"{name}min, {name}max" for name in self._axis_names)))
2107+
limits = {
2108+
name: arg[2*i:2*(i+1)]
2109+
for i, name in enumerate(self._axis_names)
2110+
}
21082111
else:
2109-
xmin = kwargs.pop('xmin', None)
2110-
xmax = kwargs.pop('xmax', None)
2111-
ymin = kwargs.pop('ymin', None)
2112-
ymax = kwargs.pop('ymax', None)
2113-
xauto = (None # Keep autoscale state as is.
2114-
if xmin is None and xmax is None
2115-
else False) # Turn off autoscale.
2116-
yauto = (None
2117-
if ymin is None and ymax is None
2118-
else False)
2119-
self.set_xlim(xmin, xmax, emit=emit, auto=xauto)
2120-
self.set_ylim(ymin, ymax, emit=emit, auto=yauto)
2112+
limits = {}
2113+
for name in self._axis_names:
2114+
ax_min = kwargs.pop(f'{name}min', None)
2115+
ax_max = kwargs.pop(f'{name}max', None)
2116+
limits[name] = (ax_min, ax_max)
2117+
for name, (ax_min, ax_max) in limits.items():
2118+
ax_auto = (None # Keep autoscale state as is.
2119+
if ax_min is None and ax_max is None
2120+
else False) # Turn off autoscale.
2121+
set_ax_lim = getattr(self, f'set_{name}lim')
2122+
set_ax_lim(ax_min, ax_max, emit=emit, auto=ax_auto)
21212123
if kwargs:
21222124
raise _api.kwarg_error("axis", kwargs)
2123-
return (*self.get_xlim(), *self.get_ylim())
2125+
lims = ()
2126+
for name in self._axis_names:
2127+
get_ax_lim = getattr(self, f'get_{name}lim')
2128+
lims += get_ax_lim()
2129+
return lims
21242130

21252131
def get_legend(self):
21262132
"""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