diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 081874fe540b..7829d57fc474 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -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: @@ -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() diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 41d604808b5d..d1158b007bec 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -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() diff --git a/lib/mpl_toolkits/mplot3d/axes3d.py b/lib/mpl_toolkits/mplot3d/axes3d.py index c307c345ef95..e8e6b7346fd1 100644 --- a/lib/mpl_toolkits/mplot3d/axes3d.py +++ b/lib/mpl_toolkits/mplot3d/axes3d.py @@ -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: @@ -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: @@ -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: diff --git a/lib/mpl_toolkits/tests/test_mplot3d.py b/lib/mpl_toolkits/tests/test_mplot3d.py index 6c9bd1f7586d..45ea08ba476c 100644 --- a/lib/mpl_toolkits/tests/test_mplot3d.py +++ b/lib/mpl_toolkits/tests/test_mplot3d.py @@ -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