From d633b8a6a0a4482f3881bd8d353168c508377985 Mon Sep 17 00:00:00 2001 From: Benjamin Congdon Date: Wed, 4 Jan 2017 19:21:58 -0500 Subject: [PATCH 1/2] Added set_xlim and set_ylim check for non-finite limit values --- doc/users/whats_new/invalid_axes_limits_errors.rst | 6 ++++++ lib/matplotlib/axes/_base.py | 10 ++++++++++ lib/matplotlib/tests/test_axes.py | 12 ++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 doc/users/whats_new/invalid_axes_limits_errors.rst diff --git a/doc/users/whats_new/invalid_axes_limits_errors.rst b/doc/users/whats_new/invalid_axes_limits_errors.rst new file mode 100644 index 000000000000..6008e2359905 --- /dev/null +++ b/doc/users/whats_new/invalid_axes_limits_errors.rst @@ -0,0 +1,6 @@ +Invalid (Non-finite) Axis Limit Error +------------------------------------- + +When using :func:`set_xlim` and :func:`set_ylim`, passing non-finite values now +results in a ValueError. The previous behavior resulted in the limits being +erroneously reset to `(-0.001, 0.001)`. diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index d4d3578b6e51..f26a5aa43213 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2875,6 +2875,11 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw): if right is not None: right = self.convert_xunits(right) + if ((left is not None and not np.isfinite(left)) or + (right is not None and not np.isfinite(right))): + raise ValueError("xlim limits must be finite. " + "instead, found: (%s, %s)" % (left, right)) + old_left, old_right = self.get_xlim() if left is None: left = old_left @@ -3169,6 +3174,11 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw): if top is not None: top = self.convert_yunits(top) + if ((top is not None and not np.isfinite(top)) or + (bottom is not None and not np.isfinite(bottom))): + raise ValueError("ylim limits must be finite. " + "instead, found: (%s, %s)" % (top, bottom)) + old_bottom, old_top = self.get_ylim() if bottom is None: diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 19cc9267b794..122619696a9a 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -4931,3 +4931,15 @@ def test_bar_single_height(): 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') + + +def test_invalid_axis_limits(): + plt.plot([0, 1], [0, 1]) + with pytest.raises(ValueError): + plt.xlim(np.nan) + with pytest.raises(ValueError): + plt.xlim(np.inf) + with pytest.raises(ValueError): + plt.ylim(np.nan) + with pytest.raises(ValueError): + plt.ylim(np.inf) From a52177aca8b12606d3ef46fba99779e71f24fb7f Mon Sep 17 00:00:00 2001 From: Benjamin Congdon Date: Mon, 6 Feb 2017 10:09:45 -0600 Subject: [PATCH 2/2] Cleaned up invalid axis limit warning text --- lib/matplotlib/axes/_base.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index f26a5aa43213..bb1f744d3fea 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2877,7 +2877,7 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw): if ((left is not None and not np.isfinite(left)) or (right is not None and not np.isfinite(right))): - raise ValueError("xlim limits must be finite. " + raise ValueError("Specified x limits must be finite; " "instead, found: (%s, %s)" % (left, right)) old_left, old_right = self.get_xlim() @@ -3176,8 +3176,8 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw): if ((top is not None and not np.isfinite(top)) or (bottom is not None and not np.isfinite(bottom))): - raise ValueError("ylim limits must be finite. " - "instead, found: (%s, %s)" % (top, bottom)) + raise ValueError("Specified y limits must be finite; " + "instead, found: (%s, %s)" % (bottom, top)) old_bottom, old_top = self.get_ylim()