From 982f5255e5da9c5023ca9997aff78c86eb0e8000 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Thu, 24 Jun 2021 01:45:04 -0400 Subject: [PATCH 5/7] Backport PR #20488: FIX: Include 0 when checking lognorm vmin Signed-off-by: Elliott Sales de Andrade --- lib/matplotlib/image.py | 6 +++--- lib/matplotlib/tests/test_image.py | 19 ++++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 98e2b5eb79..efa50758da 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -534,9 +534,9 @@ class _ImageBase(martist.Artist, cm.ScalarMappable): # we have re-set the vmin/vmax to account for small errors # that may have moved input values in/out of range s_vmin, s_vmax = vrange - if isinstance(self.norm, mcolors.LogNorm): - if s_vmin < 0: - s_vmin = max(s_vmin, np.finfo(scaled_dtype).eps) + if isinstance(self.norm, mcolors.LogNorm) and s_vmin <= 0: + # Don't give 0 or negative values to LogNorm + s_vmin = np.finfo(scaled_dtype).eps with cbook._setattr_cm(self.norm, vmin=s_vmin, vmax=s_vmax, diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 9657968de7..38182f5a8a 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -1213,23 +1213,24 @@ def test_imshow_quantitynd(): fig.canvas.draw() +@pytest.mark.parametrize('x', [-1, 1]) @check_figures_equal(extensions=['png']) -def test_huge_range_log(fig_test, fig_ref): - data = np.full((5, 5), -1, dtype=np.float64) +def test_huge_range_log(fig_test, fig_ref, x): + # parametrize over bad lognorm -1 values and large range 1 -> 1e20 + data = np.full((5, 5), x, dtype=np.float64) data[0:2, :] = 1E20 ax = fig_test.subplots() - im = ax.imshow(data, norm=colors.LogNorm(vmin=100, vmax=data.max()), - interpolation='nearest', cmap='viridis') + ax.imshow(data, norm=colors.LogNorm(vmin=1, vmax=data.max()), + interpolation='nearest', cmap='viridis') - data = np.full((5, 5), -1, dtype=np.float64) + data = np.full((5, 5), x, dtype=np.float64) data[0:2, :] = 1000 - cmap = copy(plt.get_cmap('viridis')) - cmap.set_under('w') ax = fig_ref.subplots() - im = ax.imshow(data, norm=colors.Normalize(vmin=100, vmax=data.max()), - interpolation='nearest', cmap=cmap) + cmap = plt.get_cmap('viridis').with_extremes(under='w') + ax.imshow(data, norm=colors.Normalize(vmin=1, vmax=data.max()), + interpolation='nearest', cmap=cmap) @check_figures_equal() -- 2.31.1