Skip to content

Actually ignore invalid log-axis limit setting #10950

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 4 commits into from
Apr 23, 2018
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
35 changes: 27 additions & 8 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3047,10 +3047,20 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
'left=%s, right=%s') % (left, right))
left, right = mtransforms.nonsingular(left, right, increasing=False)

if self.get_xscale() == 'log' and (left <= 0.0 or right <= 0.0):
warnings.warn(
'Attempted to set non-positive xlimits for log-scale axis; '
'invalid limits will be ignored.')
if self.get_xscale() == 'log':
if left <= 0:
warnings.warn(
'Attempted to set non-positive left xlim on a '
'log-scaled axis.\n'
'Invalid limit will be ignored.')
left = old_left
if right <= 0:
warnings.warn(
'Attempted to set non-positive right xlim on a '
'log-scaled axis.\n'
'Invalid limit will be ignored.')
right = old_right

left, right = self.xaxis.limit_range_for_scale(left, right)

self.viewLim.intervalx = (left, right)
Expand Down Expand Up @@ -3367,10 +3377,19 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):

bottom, top = mtransforms.nonsingular(bottom, top, increasing=False)

if self.get_yscale() == 'log' and (bottom <= 0.0 or top <= 0.0):
warnings.warn(
'Attempted to set non-positive ylimits for log-scale axis; '
'invalid limits will be ignored.')
if self.get_yscale() == 'log':
if bottom <= 0:
warnings.warn(
'Attempted to set non-positive bottom ylim on a '
'log-scaled axis.\n'
'Invalid limit will be ignored.')
bottom = old_bottom
if top <= 0:
warnings.warn(
'Attempted to set non-positive top ylim on a '
'log-scaled axis.\n'
'Invalid limit will be ignored.')
top = old_top
bottom, top = self.yaxis.limit_range_for_scale(bottom, top)

self.viewLim.intervaly = (bottom, top)
Expand Down
24 changes: 24 additions & 0 deletions lib/matplotlib/tests/test_scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,27 @@ def test_logscale_nonpos_values():

ax4.set_yscale('log')
ax4.set_xscale('log')


def test_invalid_log_lims():
# Check that invalid log scale limits are ignored
fig, ax = plt.subplots()
ax.scatter(range(0, 4), range(0, 4))
Copy link
Member

Choose a reason for hiding this comment

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

Are you intentionally using data points that are not part of the valid log scale? I would have taken range(1, 3).

Copy link
Member Author

Choose a reason for hiding this comment

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

No, but I don't think it matters really, as long as the limits don't change. The test fails on the master branch, so I think it's a meaningful test.

Copy link
Member

Choose a reason for hiding this comment

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

It's definitively meaningful. I just had to put in a bit of extra thought what the actual limit before set_xlim would be. So it's slightly more complicated than need be.

Change it or leave it as you wish.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think I'll leaveit.

Choose a reason for hiding this comment

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

Would this fix #10782? In that case using negative values explicitely might make sense for a test (testing for 0 and a negative value independently, I mean).

Copy link
Member Author

Choose a reason for hiding this comment

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

No, it doesn't. I think that bug is a "scatter markers don't do autoscaling" bug.


ax.set_xscale('log')
original_xlim = ax.get_xlim()
with pytest.warns(UserWarning):
ax.set_xlim(left=0)
assert ax.get_xlim() == original_xlim
with pytest.warns(UserWarning):
ax.set_xlim(right=-1)
assert ax.get_xlim() == original_xlim

ax.set_yscale('log')
original_ylim = ax.get_ylim()
with pytest.warns(UserWarning):
ax.set_ylim(bottom=0)
assert ax.get_ylim() == original_ylim
with pytest.warns(UserWarning):
ax.set_ylim(top=-1)
assert ax.get_ylim() == original_ylim