Skip to content

Commit c6c85e6

Browse files
authored
Merge pull request #20488 from greglucas/fix-image-lognorm
FIX: Include 0 when checking lognorm vmin
2 parents f292d29 + 33f3526 commit c6c85e6

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

lib/matplotlib/image.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,9 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
532532
# we have re-set the vmin/vmax to account for small errors
533533
# that may have moved input values in/out of range
534534
s_vmin, s_vmax = vrange
535-
if isinstance(self.norm, mcolors.LogNorm):
536-
if s_vmin < 0:
537-
s_vmin = max(s_vmin, np.finfo(scaled_dtype).eps)
535+
if isinstance(self.norm, mcolors.LogNorm) and s_vmin <= 0:
536+
# Don't give 0 or negative values to LogNorm
537+
s_vmin = np.finfo(scaled_dtype).eps
538538
with cbook._setattr_cm(self.norm,
539539
vmin=s_vmin,
540540
vmax=s_vmax,

lib/matplotlib/tests/test_image.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -1233,23 +1233,24 @@ def test_imshow_quantitynd():
12331233
fig.canvas.draw()
12341234

12351235

1236+
@pytest.mark.parametrize('x', [-1, 1])
12361237
@check_figures_equal(extensions=['png'])
1237-
def test_huge_range_log(fig_test, fig_ref):
1238-
data = np.full((5, 5), -1, dtype=np.float64)
1238+
def test_huge_range_log(fig_test, fig_ref, x):
1239+
# parametrize over bad lognorm -1 values and large range 1 -> 1e20
1240+
data = np.full((5, 5), x, dtype=np.float64)
12391241
data[0:2, :] = 1E20
12401242

12411243
ax = fig_test.subplots()
1242-
im = ax.imshow(data, norm=colors.LogNorm(vmin=100, vmax=data.max()),
1243-
interpolation='nearest', cmap='viridis')
1244+
ax.imshow(data, norm=colors.LogNorm(vmin=1, vmax=data.max()),
1245+
interpolation='nearest', cmap='viridis')
12441246

1245-
data = np.full((5, 5), -1, dtype=np.float64)
1247+
data = np.full((5, 5), x, dtype=np.float64)
12461248
data[0:2, :] = 1000
12471249

1248-
cmap = copy(plt.get_cmap('viridis'))
1249-
cmap.set_under('w')
12501250
ax = fig_ref.subplots()
1251-
im = ax.imshow(data, norm=colors.Normalize(vmin=100, vmax=data.max()),
1252-
interpolation='nearest', cmap=cmap)
1251+
cmap = plt.get_cmap('viridis').with_extremes(under='w')
1252+
ax.imshow(data, norm=colors.Normalize(vmin=1, vmax=data.max()),
1253+
interpolation='nearest', cmap=cmap)
12531254

12541255

12551256
@check_figures_equal()

0 commit comments

Comments
 (0)