Skip to content

Check vmin/vmax are valid when doing inverse in LogNorm #13970

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
Apr 18, 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
14 changes: 9 additions & 5 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,12 @@ def __call__(self, value, clip=None):
class LogNorm(Normalize):
"""Normalize a given value to the 0-1 range on a log scale."""

def _check_vmin_vmax(self):
if self.vmin > self.vmax:
raise ValueError("minvalue must be less than or equal to maxvalue")
elif self.vmin <= 0:
raise ValueError("minvalue must be positive")

def __call__(self, value, clip=None):
if clip is None:
clip = self.clip
Expand All @@ -1047,12 +1053,9 @@ def __call__(self, value, clip=None):
result = np.ma.masked_less_equal(result, 0, copy=False)

self.autoscale_None(result)
self._check_vmin_vmax()
vmin, vmax = self.vmin, self.vmax
if vmin > vmax:
raise ValueError("minvalue must be less than or equal to maxvalue")
elif vmin <= 0:
raise ValueError("values must all be positive")
elif vmin == vmax:
if vmin == vmax:
result.fill(0)
else:
if clip:
Expand All @@ -1078,6 +1081,7 @@ def __call__(self, value, clip=None):
def inverse(self, value):
if not self.scaled():
raise ValueError("Not invertible until scaled")
self._check_vmin_vmax()
vmin, vmax = self.vmin, self.vmax

if np.iterable(value):
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ def test_BoundaryNorm():
assert np.all(bn(vals).mask)


@pytest.mark.parametrize("vmin,vmax", [[-1, 2], [3, 1]])
def test_lognorm_invalid(vmin, vmax):
# Check that invalid limits in LogNorm error
norm = mcolors.LogNorm(vmin=vmin, vmax=vmax)
with pytest.raises(ValueError):
norm(1)
with pytest.raises(ValueError):
norm.inverse(1)


def test_LogNorm():
"""
LogNorm ignored clip, now it has the same
Expand Down