From d1b112c26e545868c33148d848091fe12b06d288 Mon Sep 17 00:00:00 2001 From: Jann Paul Mattern Date: Thu, 15 Apr 2021 22:20:06 -0700 Subject: [PATCH 1/2] fixed bug in CenteredNorm, issue #19972 --- lib/matplotlib/colors.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 698b406e758e..b835163a06f5 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -1359,6 +1359,8 @@ def __init__(self, vcenter=0, halfrange=None, clip=False): array([0.25, 0.5 , 1. ]) """ self._vcenter = vcenter + self.vmin = None + self.vmax = None # calling the halfrange setter to set vmin and vmax self.halfrange = halfrange self.clip = clip From d9aac950668aaaf8e2bc3ff892b8de25a16e9d7f Mon Sep 17 00:00:00 2001 From: Jann Paul Mattern Date: Mon, 19 Apr 2021 18:20:48 -0700 Subject: [PATCH 2/2] added test for CenteredNorm, bugfix related to #19972 --- lib/matplotlib/colors.py | 2 +- lib/matplotlib/tests/test_colors.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index b835163a06f5..91ef2fa70fb2 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -1384,7 +1384,7 @@ def autoscale(self, A): def autoscale_None(self, A): """Set *vmin* and *vmax*.""" A = np.asanyarray(A) - if self.vmax is None and A.size: + if self._halfrange is None and A.size: self.autoscale(A) @property diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index d9c31a148a9b..04ad73bfc185 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -418,6 +418,12 @@ def test_CenteredNorm(): norm.autoscale_None([1, 2]) assert norm.vmax + norm.vmin == 2 * vcenter + # Check that halfrange can be set without setting vcenter and that it is + # not reset through autoscale_None. + norm = mcolors.CenteredNorm(halfrange=1.0) + norm.autoscale_None([1, 3000]) + assert norm.halfrange == 1.0 + # Check that halfrange input works correctly. x = np.random.normal(size=10) norm = mcolors.CenteredNorm(vcenter=0.5, halfrange=0.5)