Added axis limit check for non-finite values #7744

Merged
merged 2 commits into from Mar 7, 2017
@@ -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)`.
@@ -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("Specified x 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("Specified y limits must be finite; "
+ "instead, found: (%s, %s)" % (bottom, top))
+
old_bottom, old_top = self.get_ylim()
if bottom is None:
@@ -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():
@dstansby

dstansby Feb 5, 2017

Contributor

One last thing, could a @cleanup decorator go above this line. Otherwise looks good!

@QuLogic

QuLogic Feb 5, 2017

Member

Not needed on current master.

+ 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)