GeezerVison is showing github.com — [Bug]: Plotting numpy.array of dtype float32 with pyplot.imshow and specified colors.LogNorm produces wrong colors · Issue #28731 · matplotlib/matplotlib
[Bug]: Plotting numpy.array of dtype float32 with pyplot.imshow and specified colors.LogNorm produces wrong colors #28731DescriptionBug summary
Plotting an equal numpy.array of dtype float64 and float32 with pyplot.imshow and a defined colors.LogNorm as norm will produce different colors in the resulting image.
Code for reproduction
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import numpy as np
norm = colors.LogNorm(vmin=1e-10, vmax=1)
test_numpy = np.array([[1e-4]])
test_numpy_32 = np.array([[1e-4]], dtype=np.float32)
for test in [test_numpy, test_numpy_32]:
plt.imshow(test, norm=norm)
plt.title(f"numpy array of dtype: {test.dtype}")
plt.colorbar()
plt.show()
Actual outcome
The resulting plot for the dtype float64 numpy.array looks like this:

and the plot for the dtype float32 numpy.array looks like this:

Expected outcome
I would expect both outputs to have the same color.
Additional information
No response
Operating system
Arch
Matplotlib Version
3.9.1.post1
Matplotlib Backend
module://matplotlib_inline.backend_inline
Python version
Python 3.12.4
Jupyter version
No response
Installation
pip Activitytacaswell commented on Aug 15, 2024 I can confirm this, however it is related to having singular arrays:
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import numpy as np
norm = colors.LogNorm(vmin=1e-10, vmax=1)
test_numpy = np.array([[1e-4, 1e-5, 1e-1]]).T
test_numpy_32 = np.array([[1e-4, 1e-5, 1e-1]], dtype=np.float32).T
fig, axl = plt.subplots(1, 2)
for ax, test in zip(axl, [test_numpy, test_numpy_32]):
im = ax.imshow(test, norm=norm)
ax.set_title(f"numpy array of dtype: {test.dtype}")
fig.colorbar(im)
plt.show()
works correctly. tacaswell commented on Aug 15, 2024 ax.imshow(test, norm=norm, interpolation_stage='rgba')
Also fixes the problem so it is definitely in our data-value resampling code. tacaswell commented on Aug 15, 2024 I think this is fixed by #28122 but that seems too big of a change to backport. timhoffm commented on Aug 15, 2024 I suspect that it may be an effect of the limited precision. float32 can only store 7-8 significant digits, but the log scale is set to 10 decades. Depending on how the exact calculation is done internally, the precision limit can translate to a macroscopic effect. A quick test would be to check if this goes away when limiting the log range to 6 or 7 decades. ksunden commented on Aug 15, 2024 Indeed, changing the bounds improves it significantly (I zoomed in pretty far, didn't try too many range sizes):


jklymak commented on Aug 15, 2024 This is indeed fixed on master for both interpolation_stage='rgba' (the new default) and interpolation_stage='data' (the current default). I don't think we are going to backport either #28122 or #28061, but I also don't think we should fix this just for a point release as this would be a long-standing behaviour. Currently the workaround is interpolation_stage='rgba'. I'll close as already fixed on master faressc commented on Aug 15, 2024 Thanks for the quick answers and happy to hear that it has been already fixed. I'll use the workaround interpolation_stage='rgba' for now :) changed the title [-][Bug]: Plotting numpy.arrray of dtype float32 with pyplot.imshow and specified colors.LogNorm produces wrong colors[/-] [+][Bug]: Plotting numpy.array of dtype float32 with pyplot.imshow and specified colors.LogNorm produces wrong colors[/+] on Nov 1, 2024
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|