Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix bug when setting negative limits and using log scale #14635

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
Jun 30, 2019
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
10 changes: 8 additions & 2 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3249,7 +3249,10 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False,
if right is None:
right = old_right

if self.get_xscale() == 'log':
if self.get_xscale() == 'log' and (left <= 0 or right <= 0):
Copy link
Contributor

Choose a reason for hiding this comment

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

likely the same is needed for set_ylim?

# Axes init calls set_xlim(0, 1) before get_xlim() can be called,
# so only grab the limits if we really need them.
old_left, old_right = self.get_xlim()
if left <= 0:
cbook._warn_external(
'Attempted to set non-positive left xlim on a '
Expand Down Expand Up @@ -3629,7 +3632,10 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
if top is None:
top = old_top

if self.get_yscale() == 'log':
if self.get_yscale() == 'log' and (bottom <= 0 or top <= 0):
# Axes init calls set_xlim(0, 1) before get_xlim() can be called,
# so only grab the limits if we really need them.
old_bottom, old_top = self.get_ylim()
if bottom <= 0:
cbook._warn_external(
'Attempted to set non-positive bottom ylim on a '
Expand Down
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2141,6 +2141,17 @@ def test_log_scales():
ax.set_xscale('log', basex=9.0)


def test_log_scales_invalid():
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_xscale('log')
with pytest.warns(UserWarning, match='Attempted to set non-positive'):
ax.set_xlim(-1, 10)
ax.set_yscale('log')
with pytest.warns(UserWarning, match='Attempted to set non-positive'):
ax.set_ylim(-1, 10)


@image_comparison(['stackplot_test_image', 'stackplot_test_image'])
def test_stackplot():
fig = plt.figure()
Expand Down