From ab3427931efc2610790d063db31220b48d583813 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Mon, 4 Jan 2021 21:34:47 -0800 Subject: [PATCH] FIX: process lists for inverse norms Co-authored-by: QuLogic --- lib/matplotlib/colors.py | 10 ++++++---- lib/matplotlib/tests/test_colors.py | 11 +++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index e5299bb4da48..5eeb45db4274 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -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__ diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 0bc992705a43..c17edfa1bf0a 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -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)