Skip to content

Added axis limit check for non-finite values #7744

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

Merged
merged 2 commits into from
Mar 7, 2017
Merged
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
6 changes: 6 additions & 0 deletions doc/users/whats_new/invalid_axes_limits_errors.rst
Original file line number Diff line number Diff line change
@@ -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)`.
10 changes: 10 additions & 0 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)