From 850846363734e77dc2ddcb69446cffb41eb6c725 Mon Sep 17 00:00:00 2001 From: Ryan May Date: Mon, 14 Sep 2020 14:15:01 -0600 Subject: [PATCH] Backport PR #18458: Fix huge imshow range Merge pull request #18458 from tacaswell/fix_huge_imshow_range Fix huge imshow range Conflicts: lib/matplotlib/colors.py - had spurious commit in the initial PR which re-factored code not on the 3.3.x branch lib/matplotlib/tests/test_image.py - conflicts from many tests added to bottom of test_image.py on default branch --- lib/matplotlib/image.py | 8 ++++++-- lib/matplotlib/tests/test_image.py | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 7f6a023e91a5..14866cc1012c 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -534,9 +534,13 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0, resampled_masked = np.ma.masked_array(A_resampled, out_mask) # 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) with cbook._setattr_cm(self.norm, - vmin=vrange[0], - vmax=vrange[1], + vmin=s_vmin, + vmax=s_vmax, ): output = self.norm(resampled_masked) else: diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 9e635b96d1b6..87e3c121d4c0 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -1117,3 +1117,22 @@ def test_exact_vmin(): @pytest.mark.flaky def test_https_imread_smoketest(): v = mimage.imread('https://matplotlib.org/1.5.0/_static/logo2.png') + + +@check_figures_equal(extensions=['png']) +def test_huge_range_log(fig_test, fig_ref): + data = np.full((5, 5), -1, 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') + + data = np.full((5, 5), -1, dtype=np.float64) + data[0:2, :] = 1000 + + cm = copy(plt.get_cmap('viridis')) + cm.set_under('w') + ax = fig_ref.subplots() + im = ax.imshow(data, norm=colors.Normalize(vmin=100, vmax=data.max()), + interpolation='nearest', cmap=cm)