From a00a9097c2c502395b382b8adf87ce9654323a4b Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sat, 18 Sep 2021 17:02:32 +0200 Subject: [PATCH] Fix make_norm_from_scale `__name__` when used inline. `make_norm_from_scale` can be used "inline", rather than as a class decorator, to dynamically create norm classes. However, in that case, the second parameter (`base_norm_cls`) would normally be set to the root base norm class (`mcolors.Normalize`). In that case, we should generate a new `__name__` for the dynamically generated class, to avoid the slightly confusing situation of having two different classes both called `mcolors.Normalize`. --- lib/matplotlib/colors.py | 3 ++- lib/matplotlib/tests/test_colors.py | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 0843bd5f592c..f5081825c2ab 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -1561,7 +1561,8 @@ def inverse(self, value): .reshape(np.shape(value))) return value[0] if is_scalar else value - Norm.__name__ = base_norm_cls.__name__ + Norm.__name__ = (f"{scale_cls.__name__}Norm" if base_norm_cls is Normalize + else base_norm_cls.__name__) Norm.__qualname__ = base_norm_cls.__qualname__ Norm.__module__ = base_norm_cls.__module__ Norm.__doc__ = base_norm_cls.__doc__ diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index bf89a3a82364..4fa65918e7fa 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -1475,3 +1475,9 @@ def test_norm_update_figs(fig_test, fig_ref): # Force initial draw to make sure it isn't already stale fig_test.canvas.draw() norm.vmin, norm.vmax = 10, 90 + + +def test_make_norm_from_scale_name(): + logitnorm = mcolors.make_norm_from_scale( + mscale.LogitScale, mcolors.Normalize) + assert logitnorm.__name__ == "LogitScaleNorm"