Skip to content

imshow with LogNorm and large data ranges: pixelation and crashes #19851

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

Closed
chrishavlin opened this issue Apr 2, 2021 · 2 comments
Closed

imshow with LogNorm and large data ranges: pixelation and crashes #19851

chrishavlin opened this issue Apr 2, 2021 · 2 comments

Comments

@chrishavlin
Copy link

Bug report

Bug summary

Using imshow with LogNorm scaling and a large range results in pixelation and crashes. This is related to Issue #18415 but the fix that went in there doesn't seem to be general enough.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm

data = np.full((500, 500), 1e30, dtype=np.float64)
data[0:250, :] = 1e12

fig, ax = plt.subplots()
im = ax.imshow(data, norm=LogNorm())
plt.colorbar(im)
plt.show()

Results in

Screenshot from 2021-04-02 11-59-57

Expected outcome

Outcome should be similar to the case with a slightly smaller data range:

data = np.full((500, 500), 1e30, dtype=np.float64)
data[0:250, :] = 1e13

fig, ax = plt.subplots()
im = ax.imshow(np.log10(data))
plt.colorbar(im)
plt.show()

Screenshot from 2021-04-02 12-04-12

An additional manifestation of this bug that causes a crash is when the data contains a zero point, the vmin rescaling still results in a 0 value even when setting vmin:

data = np.full((500, 500), 1e30, dtype=np.float64)
data[0:250, :] = 1e12
data[0,0] = 0.

fig, ax = plt.subplots()
im = ax.imshow(data, norm=LogNorm(vmin=1e12, vmax=1e30))
plt.show()

results in

~/.pyenv/versions/3.8.0/envs/sandbox/lib/python3.8/site-packages/matplotlib/colors.py in __call__(self, value, clip)
   1463             t_vmin, t_vmax = self._trf.transform([self.vmin, self.vmax])
   1464             if not np.isfinite([t_vmin, t_vmax]).all():
-> 1465                 raise ValueError("Invalid vmin or vmax")
   1466             t_value -= t_vmin
   1467             t_value /= (t_vmax - t_vmin)

ValueError: Invalid vmin or vmax

Using a smaller range for vmin and vmax avoids the error but results in a pixelated plot:

data = np.full((500, 500), 1e30, dtype=np.float64)
data[0:250, :] = 1e12
data[0,0] = 0.

fig, ax = plt.subplots()
im = ax.imshow(data, norm=LogNorm(vmin=1e20, vmax=1e30))
plt.show()

Screenshot from 2021-04-02 12-10-02

Matplotlib version
matplotlib 3.4.1

@chrishavlin
Copy link
Author

Oh, I should have searched the open issue more thoroughly -- I think #19748 and the related fix going in may cover the case here.

@jklymak
Copy link
Member

jklymak commented Apr 2, 2021

yep! Feel free to comment there!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants