Skip to content

Fix huge imshow range #18458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,6 @@ def __init__(self, *args, **kwargs):
**{k: ba.arguments.pop(k) for k in ["vmin", "vmax", "clip"]})
self._scale = scale_cls(axis=None, **ba.arguments)
self._trf = self._scale.get_transform()
self._inv_trf = self._trf.inverted()

def __call__(self, value, clip=None):
value, is_scalar = self.process_value(value)
Expand Down Expand Up @@ -1283,7 +1282,10 @@ def inverse(self, value):
raise ValueError("Invalid vmin or vmax")
rescaled = value * (t_vmax - t_vmin)
rescaled += t_vmin
return self._inv_trf.transform(rescaled).reshape(np.shape(value))
return (self._trf
.inverted()
.transform(rescaled)
.reshape(np.shape(value)))

Norm.__name__ = base_norm_cls.__name__
Norm.__qualname__ = base_norm_cls.__qualname__
Expand Down
8 changes: 6 additions & 2 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 19 additions & 0 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,3 +1204,22 @@ def test_imshow_quantitynd():
ax.imshow(arr)
# executing the draw should not raise an exception
fig.canvas.draw()


@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)