Skip to content

Commit b840d3a

Browse files
authored
Merge pull request #13970 from dstansby/lognorm-inverse-check
Check vmin/vmax are valid when doing inverse in LogNorm
2 parents 76db501 + c56c551 commit b840d3a

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

lib/matplotlib/colors.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,12 @@ def __call__(self, value, clip=None):
10381038
class LogNorm(Normalize):
10391039
"""Normalize a given value to the 0-1 range on a log scale."""
10401040

1041+
def _check_vmin_vmax(self):
1042+
if self.vmin > self.vmax:
1043+
raise ValueError("minvalue must be less than or equal to maxvalue")
1044+
elif self.vmin <= 0:
1045+
raise ValueError("minvalue must be positive")
1046+
10411047
def __call__(self, value, clip=None):
10421048
if clip is None:
10431049
clip = self.clip
@@ -1047,12 +1053,9 @@ def __call__(self, value, clip=None):
10471053
result = np.ma.masked_less_equal(result, 0, copy=False)
10481054

10491055
self.autoscale_None(result)
1056+
self._check_vmin_vmax()
10501057
vmin, vmax = self.vmin, self.vmax
1051-
if vmin > vmax:
1052-
raise ValueError("minvalue must be less than or equal to maxvalue")
1053-
elif vmin <= 0:
1054-
raise ValueError("values must all be positive")
1055-
elif vmin == vmax:
1058+
if vmin == vmax:
10561059
result.fill(0)
10571060
else:
10581061
if clip:
@@ -1078,6 +1081,7 @@ def __call__(self, value, clip=None):
10781081
def inverse(self, value):
10791082
if not self.scaled():
10801083
raise ValueError("Not invertible until scaled")
1084+
self._check_vmin_vmax()
10811085
vmin, vmax = self.vmin, self.vmax
10821086

10831087
if np.iterable(value):

lib/matplotlib/tests/test_colors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ def test_BoundaryNorm():
145145
assert np.all(bn(vals).mask)
146146

147147

148+
@pytest.mark.parametrize("vmin,vmax", [[-1, 2], [3, 1]])
149+
def test_lognorm_invalid(vmin, vmax):
150+
# Check that invalid limits in LogNorm error
151+
norm = mcolors.LogNorm(vmin=vmin, vmax=vmax)
152+
with pytest.raises(ValueError):
153+
norm(1)
154+
with pytest.raises(ValueError):
155+
norm.inverse(1)
156+
157+
148158
def test_LogNorm():
149159
"""
150160
LogNorm ignored clip, now it has the same

0 commit comments

Comments
 (0)