Skip to content

FIX: process lists for inverse norms #19240

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 1 commit into from
Jan 5, 2021
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: 6 additions & 4 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1449,12 +1449,14 @@ def inverse(self, value):
t_vmin, t_vmax = self._trf.transform([self.vmin, self.vmax])
if not np.isfinite([t_vmin, t_vmax]).all():
raise ValueError("Invalid vmin or vmax")
value, is_scalar = self.process_value(value)
rescaled = value * (t_vmax - t_vmin)
rescaled += t_vmin
return (self._trf
.inverted()
.transform(rescaled)
.reshape(np.shape(value)))
value = (self._trf
.inverted()
.transform(rescaled)
.reshape(np.shape(value)))
return value[0] if is_scalar else value

Norm.__name__ = base_norm_cls.__name__
Norm.__qualname__ = base_norm_cls.__qualname__
Expand Down
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,17 @@ def test_LogNorm():
assert_array_equal(ln([1, 6]), [0, 1.0])


def test_LogNorm_inverse():
"""
Test that lists work, and that the inverse works
"""
norm = mcolors.LogNorm(vmin=0.1, vmax=10)
assert_array_almost_equal(norm([0.5, 0.4]), [0.349485, 0.30103])
assert_array_almost_equal([0.5, 0.4], norm.inverse([0.349485, 0.30103]))
assert_array_almost_equal(norm(0.4), [0.30103])
assert_array_almost_equal([0.4], norm.inverse([0.30103]))


def test_PowerNorm():
a = np.array([0, 0.5, 1, 1.5], dtype=float)
pnorm = mcolors.PowerNorm(1)
Expand Down