Repository navigation
Description
When plotting a 2D array with imshow and cmap="fire", NaN values appear nearly black instead of transparent.
I encountered this when replacing nonpositive values with NaN for logarithmic normalization.
Minimal example
import numpy as np
import ultraplot as uplt
data = np.array([
[np.nan, np.nan, np.nan],
[np.nan, 0.1, 1.0],
[np.nan, 10.0, 100.0],
])
fig, ax = uplt.subplots()
im = ax.imshow(
data,
cmap="fire",
norm="log",
vmin=0.1,
vmax=100.0,
)
print(im.cmap.get_bad())
# Approximately [0.0148, 0.0142, 0.0142, 1.0], rather than transparent.
fig.show()Expected behavior
NaN values should be transparent by default.
Environment
- UltraPlot 2.7.2
- Matplotlib 3.11.2
- Python 3.14
Possible cause
PerceptualColormap._init() in UltraPlot 2.7.2 calls _set_extremes() before setting _isinit = True.
The relevant Matplotlib implementation changed between these versions:
- Matplotlib 3.10.9:
_set_extremes()writes the special colors directly into the lookup table, without checking_isinit. - Matplotlib 3.11.2:
_set_extremes()calls_update_lut_extremes()only whenself._isinitis true. - The actual lookup-table writes are now in
_update_lut_extremes().
Consequently, on initial construction with Matplotlib 3.11.2, UltraPlot's call to _set_extremes() does not populate the special-color entries.
Those entries retain the initial np.ones values, including alpha = 1, and are then converted from HSL to RGB, producing an opaque, nearly black color.
The colormap's _rgba_bad is still (0, 0, 0, 0), but get_bad() returns an opaque, nearly black color.
In a process-local comparison, replacing only Matplotlib's _set_extremes() implementation with the one from 3.10.9 restored transparent NaN colors.
Edited: corrected both source-code links and added the version comparison above. The original line anchors pointed to unrelated code.
Workaround
Setting the bad color after initialization restores transparency. This was verified through rendering:
im.cmap.get_bad() # Ensure initialization.
im.cmap.set_bad("none")Related issues
Related: #831, but this concerns NaN transparency rather than colormap lookup.
I searched existing open and closed issues and pull requests and did not find a matching report.
Activity
cvanelteren commentedon Oct 19, 2026
@munechika-koyo the link to the code is pointing to
__eq__which seems to be not related. Do you know what changed in mpl versions?munechika-koyo commentedon Oct 20, 2026
@cvanelteren, I am sorry. I fixed the link to the correct one.
cvanelteren commentedon Oct 20, 2026
No worries fix is already on the listed PR