Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0b21c7c

Browse files
authored
Merge pull request #18458 from tacaswell/fix_huge_imshow_range
Fix huge imshow range
2 parents 80550e1 + b23708a commit 0b21c7c

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

lib/matplotlib/colors.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,6 @@ def __init__(self, *args, **kwargs):
12861286
**{k: ba.arguments.pop(k) for k in ["vmin", "vmax", "clip"]})
12871287
self._scale = scale_cls(axis=None, **ba.arguments)
12881288
self._trf = self._scale.get_transform()
1289-
self._inv_trf = self._trf.inverted()
12901289

12911290
def __call__(self, value, clip=None):
12921291
value, is_scalar = self.process_value(value)
@@ -1318,7 +1317,10 @@ def inverse(self, value):
13181317
raise ValueError("Invalid vmin or vmax")
13191318
rescaled = value * (t_vmax - t_vmin)
13201319
rescaled += t_vmin
1321-
return self._inv_trf.transform(rescaled).reshape(np.shape(value))
1320+
return (self._trf
1321+
.inverted()
1322+
.transform(rescaled)
1323+
.reshape(np.shape(value)))
13221324

13231325
Norm.__name__ = base_norm_cls.__name__
13241326
Norm.__qualname__ = base_norm_cls.__qualname__

lib/matplotlib/image.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,13 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
530530
resampled_masked = np.ma.masked_array(A_resampled, out_mask)
531531
# we have re-set the vmin/vmax to account for small errors
532532
# that may have moved input values in/out of range
533+
s_vmin, s_vmax = vrange
534+
if isinstance(self.norm, mcolors.LogNorm):
535+
if s_vmin < 0:
536+
s_vmin = max(s_vmin, np.finfo(scaled_dtype).eps)
533537
with cbook._setattr_cm(self.norm,
534-
vmin=vrange[0],
535-
vmax=vrange[1],
538+
vmin=s_vmin,
539+
vmax=s_vmax,
536540
):
537541
output = self.norm(resampled_masked)
538542
else:

lib/matplotlib/tests/test_image.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,3 +1209,22 @@ def test_imshow_quantitynd():
12091209
ax.imshow(arr)
12101210
# executing the draw should not raise an exception
12111211
fig.canvas.draw()
1212+
1213+
1214+
@check_figures_equal(extensions=['png'])
1215+
def test_huge_range_log(fig_test, fig_ref):
1216+
data = np.full((5, 5), -1, dtype=np.float64)
1217+
data[0:2, :] = 1E20
1218+
1219+
ax = fig_test.subplots()
1220+
im = ax.imshow(data, norm=colors.LogNorm(vmin=100, vmax=data.max()),
1221+
interpolation='nearest', cmap='viridis')
1222+
1223+
data = np.full((5, 5), -1, dtype=np.float64)
1224+
data[0:2, :] = 1000
1225+
1226+
cm = copy(plt.get_cmap('viridis'))
1227+
cm.set_under('w')
1228+
ax = fig_ref.subplots()
1229+
im = ax.imshow(data, norm=colors.Normalize(vmin=100, vmax=data.max()),
1230+
interpolation='nearest', cmap=cm)

0 commit comments

Comments
 (0)