Skip to content

Commit 43bc27d

Browse files
authored
Merge pull request #18484 from tacaswell/auto-backport-of-pr-18458-on-v3.3.x
Backport PR #18458: Fix huge imshow range
2 parents 254f062 + 8508463 commit 43bc27d

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/matplotlib/image.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -534,9 +534,13 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
534534
resampled_masked = np.ma.masked_array(A_resampled, out_mask)
535535
# we have re-set the vmin/vmax to account for small errors
536536
# that may have moved input values in/out of range
537+
s_vmin, s_vmax = vrange
538+
if isinstance(self.norm, mcolors.LogNorm):
539+
if s_vmin < 0:
540+
s_vmin = max(s_vmin, np.finfo(scaled_dtype).eps)
537541
with cbook._setattr_cm(self.norm,
538-
vmin=vrange[0],
539-
vmax=vrange[1],
542+
vmin=s_vmin,
543+
vmax=s_vmax,
540544
):
541545
output = self.norm(resampled_masked)
542546
else:

lib/matplotlib/tests/test_image.py

+19
Original file line numberDiff line numberDiff line change
@@ -1117,3 +1117,22 @@ def test_exact_vmin():
11171117
@pytest.mark.flaky
11181118
def test_https_imread_smoketest():
11191119
v = mimage.imread('https://matplotlib.org/1.5.0/_static/logo2.png')
1120+
1121+
1122+
@check_figures_equal(extensions=['png'])
1123+
def test_huge_range_log(fig_test, fig_ref):
1124+
data = np.full((5, 5), -1, dtype=np.float64)
1125+
data[0:2, :] = 1E20
1126+
1127+
ax = fig_test.subplots()
1128+
im = ax.imshow(data, norm=colors.LogNorm(vmin=100, vmax=data.max()),
1129+
interpolation='nearest', cmap='viridis')
1130+
1131+
data = np.full((5, 5), -1, dtype=np.float64)
1132+
data[0:2, :] = 1000
1133+
1134+
cm = copy(plt.get_cmap('viridis'))
1135+
cm.set_under('w')
1136+
ax = fig_ref.subplots()
1137+
im = ax.imshow(data, norm=colors.Normalize(vmin=100, vmax=data.max()),
1138+
interpolation='nearest', cmap=cm)

0 commit comments

Comments
 (0)