Skip to content

Raise errors on invalid axis limits #8216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2873,10 +2873,15 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
left, right = left

self._process_unit_info(xdata=(left, right))

if left is not None:
left = self.convert_xunits(left)
if ((np.isscalar(left)) and not (np.isfinite(left))):
raise ValueError('left/right should be finite values')
if right is not None:
right = self.convert_xunits(right)
if ((np.isscalar(right)) and (not np.isfinite(right))):
raise ValueError('left/right should be finite values')

old_left, old_right = self.get_xlim()
if left is None:
Expand Down Expand Up @@ -3169,8 +3174,12 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):

if bottom is not None:
bottom = self.convert_yunits(bottom)
if ((np.isscalar(bottom)) and not (np.isfinite(bottom))):
raise ValueError('top/bottom should be finite values')
if top is not None:
top = self.convert_yunits(top)
if ((np.isscalar(top)) and (not np.isfinite(top))):
raise ValueError('top/bottom should be finite values')

old_bottom, old_top = self.get_ylim()

Expand Down
26 changes: 26 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,32 @@ def test_pcolorargs():
ax.pcolormesh(X, Y, Z[:-1, :-1], shading="gouraud")


def test_axes():
fig = plt.figure()
x = np.linspace(0, 10, 10)
y1 = 1.0 * x
y2 = 2.0 * x + 1
y3 = 3.0 * x + 2
ax = fig.add_subplot(1, 1, 1)
ax.stackplot(x, y1, y2, y3)
with pytest.raises(ValueError):
ax.set_xlim(left=np.nan)
with pytest.raises(ValueError):
ax.set_xlim(left=np.inf)
with pytest.raises(ValueError):
ax.set_xlim(right=np.nan)
with pytest.raises(ValueError):
ax.set_xlim(right=np.inf)
with pytest.raises(ValueError):
ax.set_ylim(top=np.nan)
with pytest.raises(ValueError):
ax.set_ylim(top=np.inf)
with pytest.raises(ValueError):
ax.set_ylim(bottom=np.nan)
with pytest.raises(ValueError):
ax.set_ylim(bottom=np.inf)


@image_comparison(baseline_images=['canonical'])
def test_canonical():
fig, ax = plt.subplots()
Expand Down
13 changes: 13 additions & 0 deletions lib/mpl_toolkits/mplot3d/axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,12 @@ def set_xlim3d(self, left=None, right=None, emit=True, auto=False, **kw):
self._process_unit_info(xdata=(left, right))
if left is not None:
left = self.convert_xunits(left)
if ((np.isscalar(left)) and not (np.isfinite(left))):
raise ValueError('left/right should be finite values')
if right is not None:
right = self.convert_xunits(right)
if ((np.isscalar(right)) and (not np.isfinite(right))):
raise ValueError('left/right should be finite values')

old_left, old_right = self.get_xlim()
if left is None:
Expand Down Expand Up @@ -659,8 +663,12 @@ def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
self._process_unit_info(ydata=(bottom, top))
if bottom is not None:
bottom = self.convert_yunits(bottom)
if ((np.isscalar(bottom)) and not (np.isfinite(bottom))):
raise ValueError('bottom should be finite values')
if top is not None:
top = self.convert_yunits(top)
if ((np.isscalar(top)) and (not np.isfinite(top))):
raise ValueError('top should be finite values')

old_bottom, old_top = self.get_ylim()
if bottom is None:
Expand Down Expand Up @@ -711,10 +719,15 @@ def set_zlim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
bottom, top = bottom

self._process_unit_info(zdata=(bottom, top))

if bottom is not None:
bottom = self.convert_zunits(bottom)
if ((np.isscalar(bottom)) and not (np.isfinite(bottom))):
raise ValueError('bottom should be finite values')
if top is not None:
top = self.convert_zunits(top)
if ((np.isscalar(top)) and (not np.isfinite(top))):
raise ValueError('top should be finite values')

old_bottom, old_top = self.get_zlim()
if bottom is None:
Expand Down
44 changes: 44 additions & 0 deletions lib/mpl_toolkits/tests/test_mplot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,50 @@ def test_text3d():
ax.set_zlabel('Z axis')


def test_axes3d():
fig = plt.figure()
ax = fig.gca(projection='3d')

zdirs = (None, 'x', 'y', 'z', (1, 1, 0), (1, 1, 1))
xs = (2, 6, 4, 9, 7, 2)
ys = (6, 4, 8, 7, 2, 2)
zs = (4, 2, 5, 6, 1, 7)

for zdir, x, y, z in zip(zdirs, xs, ys, zs):
label = '(%d, %d, %d), dir=%s' % (x, y, z, zdir)
ax.text(x, y, z, label, zdir)

ax.text(1, 1, 1, "red", color='red')
ax.text2D(0.05, 0.95, "2D Text", transform=ax.transAxes)

with pytest.raises(ValueError):
ax.set_xlim3d(left=np.nan)
with pytest.raises(ValueError):
ax.set_xlim3d(left=np.inf)
with pytest.raises(ValueError):
ax.set_xlim3d(right=np.nan)
with pytest.raises(ValueError):
ax.set_xlim3d(right=np.inf)

with pytest.raises(ValueError):
ax.set_ylim3d(top=np.nan)
with pytest.raises(ValueError):
ax.set_ylim3d(top=np.inf)
with pytest.raises(ValueError):
ax.set_ylim3d(bottom=np.nan)
with pytest.raises(ValueError):
ax.set_ylim3d(bottom=np.inf)

with pytest.raises(ValueError):
ax.set_zlim3d(top=np.nan)
with pytest.raises(ValueError):
ax.set_zlim3d(top=np.inf)
with pytest.raises(ValueError):
ax.set_zlim3d(bottom=np.nan)
with pytest.raises(ValueError):
ax.set_zlim3d(bottom=np.inf)


@image_comparison(baseline_images=['trisurf3d'], remove_text=True, tol=0.03)
def test_trisurf3d():
n_angles = 36
Expand Down