From 677e97d112fa0c380486430bceb3524eb87d55b8 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Tue, 3 Apr 2018 16:53:18 +0100 Subject: [PATCH 1/4] Actually ignore invalid log-axis limit setting --- lib/matplotlib/axes/_base.py | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index b771474c585b..ddd27adac269 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -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.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.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) @@ -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.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.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) From 625283f7e0d253a8ac1fb0d68b0962594e2a6ec6 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Wed, 4 Apr 2018 12:39:24 +0100 Subject: [PATCH 2/4] Add test to check invalid limits ignored on log scale --- lib/matplotlib/tests/test_scale.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/matplotlib/tests/test_scale.py b/lib/matplotlib/tests/test_scale.py index c50a0a89609c..46a1e1e92458 100644 --- a/lib/matplotlib/tests/test_scale.py +++ b/lib/matplotlib/tests/test_scale.py @@ -121,3 +121,21 @@ 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)) + + ax.set_xscale('log') + original_xlim = ax.get_xlim() + with pytest.warns(UserWarning): + ax.set_xlim(left=0) + 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 From c0a0873a441312a515e434ae0f598f39b501dba2 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Wed, 4 Apr 2018 12:40:52 +0100 Subject: [PATCH 3/4] Test right and top too --- lib/matplotlib/tests/test_scale.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/matplotlib/tests/test_scale.py b/lib/matplotlib/tests/test_scale.py index 46a1e1e92458..8f3e80e04df6 100644 --- a/lib/matplotlib/tests/test_scale.py +++ b/lib/matplotlib/tests/test_scale.py @@ -133,9 +133,15 @@ def test_invalid_log_lims(): 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 From a5b071bbcd46c1dbffe9ac671350257a3a065114 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Fri, 20 Apr 2018 11:42:23 +0100 Subject: [PATCH 4/4] 0.0 --> 0 --- lib/matplotlib/axes/_base.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index ddd27adac269..0911d6295818 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -3048,13 +3048,13 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw): left, right = mtransforms.nonsingular(left, right, increasing=False) if self.get_xscale() == 'log': - if left <= 0.0: + 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.0: + if right <= 0: warnings.warn( 'Attempted to set non-positive right xlim on a ' 'log-scaled axis.\n' @@ -3378,13 +3378,13 @@ 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': - if bottom <= 0.0: + 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.0: + if top <= 0: warnings.warn( 'Attempted to set non-positive top ylim on a ' 'log-scaled axis.\n'