From 1745d4a5fd17252c85dc663f28e42f29c644e715 Mon Sep 17 00:00:00 2001 From: Chen Jui Ray Chu Date: Mon, 6 Mar 2017 23:50:24 -0500 Subject: [PATCH 1/4] Issue 7460 fixed --- lib/matplotlib/axes/_base.py | 11 +++++- lib/matplotlib/tests/test_axes.py | 27 ++++++++++++++- lib/mpl_toolkits/mplot3d/axes3d.py | 15 ++++++++- lib/mpl_toolkits/tests/test_mplot3d.py | 46 +++++++++++++++++++++++++- 4 files changed, 95 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 081874fe540b..4a9b6012205a 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2873,6 +2873,11 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw): left, right = left self._process_unit_info(xdata=(left, right)) + + if ((np.isscalar(left)) and not (np.isfinite(left))) \ + or ((np.isscalar(right)) and (not np.isfinite(right))): + raise ValueError('left/right should be finite values') + if left is not None: left = self.convert_xunits(left) if right is not None: @@ -3167,6 +3172,10 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw): if top is None and iterable(bottom): bottom, top = bottom + if ((np.isscalar(top)) and not (np.isfinite(top))) \ + or ((np.isscalar(bottom)) and (not np.isfinite(bottom))): + raise ValueError('top/bottom should be finite values') + if bottom is not None: bottom = self.convert_yunits(bottom) if top is not None: @@ -3966,4 +3975,4 @@ def get_shared_x_axes(self): def get_shared_y_axes(self): 'Return a copy of the shared axes Grouper object for y axes' - return self._shared_y_axes + return self._shared_y_axes \ No newline at end of file diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 41d604808b5d..cc36dca9ddac 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -996,6 +996,31 @@ 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() @@ -4971,4 +4996,4 @@ def test_bar_single_height(): # Check that a bar chart with a single height for all bars works ax.bar(range(4), 1) # Check that a horizontal chart with one width works - ax.bar(0, 1, bottom=range(4), width=1, orientation='horizontal') + ax.bar(0, 1, bottom=range(4), width=1, orientation='horizontal') \ No newline at end of file diff --git a/lib/mpl_toolkits/mplot3d/axes3d.py b/lib/mpl_toolkits/mplot3d/axes3d.py index c307c345ef95..f476a54d1304 100644 --- a/lib/mpl_toolkits/mplot3d/axes3d.py +++ b/lib/mpl_toolkits/mplot3d/axes3d.py @@ -602,6 +602,10 @@ def set_xlim3d(self, left=None, right=None, emit=True, auto=False, **kw): if right is None and cbook.iterable(left): left, right = left + if ((np.isscalar(left)) and not (np.isfinite(left))) \ + or ((np.isscalar(right)) and (not np.isfinite(right))): + raise ValueError('left/right should be finite values') + self._process_unit_info(xdata=(left, right)) if left is not None: left = self.convert_xunits(left) @@ -656,6 +660,10 @@ def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False, **kw): if top is None and cbook.iterable(bottom): bottom, top = bottom + if ((np.isscalar(top)) and not (np.isfinite(top))) \ + or ((np.isscalar(bottom)) and (not np.isfinite(bottom))): + raise ValueError('top/bottom should be finite values') + self._process_unit_info(ydata=(bottom, top)) if bottom is not None: bottom = self.convert_yunits(bottom) @@ -711,6 +719,11 @@ def set_zlim3d(self, bottom=None, top=None, emit=True, auto=False, **kw): bottom, top = bottom self._process_unit_info(zdata=(bottom, top)) + + if ((np.isscalar(top)) and not (np.isfinite(top))) \ + or ((np.isscalar(bottom)) and (not np.isfinite(bottom))): + raise ValueError('top/bottom should be finite values') + if bottom is not None: bottom = self.convert_zunits(bottom) if top is not None: @@ -2733,4 +2746,4 @@ def get_test_data(delta=0.05): # for use just like any other axes ######################################################## import matplotlib.projections as proj -proj.projection_registry.register(Axes3D) +proj.projection_registry.register(Axes3D) \ No newline at end of file diff --git a/lib/mpl_toolkits/tests/test_mplot3d.py b/lib/mpl_toolkits/tests/test_mplot3d.py index 6c9bd1f7586d..21c998f790a1 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 @@ -490,4 +534,4 @@ def test_autoscale(): ax.autoscale(False) ax.set_autoscalez_on(True) ax.plot([0, 2], [0, 2], [0, 2]) - assert ax.get_w_lims() == (0, 1, -.1, 1.1, -.4, 2.4) + assert ax.get_w_lims() == (0, 1, -.1, 1.1, -.4, 2.4) \ No newline at end of file From dc7278c290793dac29e73d2645731353104d3b99 Mon Sep 17 00:00:00 2001 From: Chen Jui Ray Chu Date: Mon, 6 Mar 2017 23:54:46 -0500 Subject: [PATCH 2/4] Added newlines that were somehow missing --- lib/matplotlib/axes/_base.py | 2 +- lib/matplotlib/tests/test_axes.py | 2 +- lib/mpl_toolkits/tests/test_mplot3d.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 4a9b6012205a..8874372433e9 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -3975,4 +3975,4 @@ def get_shared_x_axes(self): def get_shared_y_axes(self): 'Return a copy of the shared axes Grouper object for y axes' - return self._shared_y_axes \ No newline at end of file + return self._shared_y_axes diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index cc36dca9ddac..5af0fae58073 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -4996,4 +4996,4 @@ def test_bar_single_height(): # Check that a bar chart with a single height for all bars works ax.bar(range(4), 1) # Check that a horizontal chart with one width works - ax.bar(0, 1, bottom=range(4), width=1, orientation='horizontal') \ No newline at end of file + ax.bar(0, 1, bottom=range(4), width=1, orientation='horizontal') diff --git a/lib/mpl_toolkits/tests/test_mplot3d.py b/lib/mpl_toolkits/tests/test_mplot3d.py index 21c998f790a1..45ea08ba476c 100644 --- a/lib/mpl_toolkits/tests/test_mplot3d.py +++ b/lib/mpl_toolkits/tests/test_mplot3d.py @@ -534,4 +534,4 @@ def test_autoscale(): ax.autoscale(False) ax.set_autoscalez_on(True) ax.plot([0, 2], [0, 2], [0, 2]) - assert ax.get_w_lims() == (0, 1, -.1, 1.1, -.4, 2.4) \ No newline at end of file + assert ax.get_w_lims() == (0, 1, -.1, 1.1, -.4, 2.4) From 77715d87823d9c6f0c4781333f3bb9c2936f1965 Mon Sep 17 00:00:00 2001 From: raychut Date: Mon, 6 Mar 2017 23:58:11 -0500 Subject: [PATCH 3/4] Update axes3d.py --- lib/mpl_toolkits/mplot3d/axes3d.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mpl_toolkits/mplot3d/axes3d.py b/lib/mpl_toolkits/mplot3d/axes3d.py index f476a54d1304..1dbb1b9cb255 100644 --- a/lib/mpl_toolkits/mplot3d/axes3d.py +++ b/lib/mpl_toolkits/mplot3d/axes3d.py @@ -2746,4 +2746,4 @@ def get_test_data(delta=0.05): # for use just like any other axes ######################################################## import matplotlib.projections as proj -proj.projection_registry.register(Axes3D) \ No newline at end of file +proj.projection_registry.register(Axes3D) From 7643d354b5f9b5ed89d945ec901cde284b45e4d6 Mon Sep 17 00:00:00 2001 From: Alix Lian Date: Wed, 8 Mar 2017 20:05:55 -0500 Subject: [PATCH 4/4] removed \, and moved error check after convert unit Passed pep8 --- lib/matplotlib/axes/_base.py | 16 ++++++++-------- lib/matplotlib/tests/test_axes.py | 1 + lib/mpl_toolkits/mplot3d/axes3d.py | 24 ++++++++++++------------ 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 8874372433e9..7829d57fc474 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2874,14 +2874,14 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw): self._process_unit_info(xdata=(left, right)) - if ((np.isscalar(left)) and not (np.isfinite(left))) \ - or ((np.isscalar(right)) and (not np.isfinite(right))): - raise ValueError('left/right should be finite values') - 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: @@ -3172,14 +3172,14 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw): if top is None and iterable(bottom): bottom, top = bottom - if ((np.isscalar(top)) and not (np.isfinite(top))) \ - or ((np.isscalar(bottom)) and (not np.isfinite(bottom))): - raise ValueError('top/bottom should be finite values') - 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 5af0fae58073..d1158b007bec 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1021,6 +1021,7 @@ def test_axes(): 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 1dbb1b9cb255..e8e6b7346fd1 100644 --- a/lib/mpl_toolkits/mplot3d/axes3d.py +++ b/lib/mpl_toolkits/mplot3d/axes3d.py @@ -602,15 +602,15 @@ def set_xlim3d(self, left=None, right=None, emit=True, auto=False, **kw): if right is None and cbook.iterable(left): left, right = left - if ((np.isscalar(left)) and not (np.isfinite(left))) \ - or ((np.isscalar(right)) and (not np.isfinite(right))): - raise ValueError('left/right should be finite values') - 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: @@ -660,15 +660,15 @@ def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False, **kw): if top is None and cbook.iterable(bottom): bottom, top = bottom - if ((np.isscalar(top)) and not (np.isfinite(top))) \ - or ((np.isscalar(bottom)) and (not np.isfinite(bottom))): - raise ValueError('top/bottom should be finite values') - 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: @@ -720,14 +720,14 @@ def set_zlim3d(self, bottom=None, top=None, emit=True, auto=False, **kw): self._process_unit_info(zdata=(bottom, top)) - if ((np.isscalar(top)) and not (np.isfinite(top))) \ - or ((np.isscalar(bottom)) and (not np.isfinite(bottom))): - raise ValueError('top/bottom should be finite values') - 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: