Skip to content

NaN values appear nearly black with fire on Matplotlib 3.11 #848

Closed

Description

@munechika-koyo

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:

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

  1. cvanelteren commented on Oct 19, 2026

    @cvanelteren
    Collaborator

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

  2. munechika-koyo commented on Oct 20, 2026

    @munechika-koyo
    CollaboratorAuthor

    @cvanelteren, I am sorry. I fixed the link to the correct one.

  3. cvanelteren commented on Oct 20, 2026

    @cvanelteren
    Collaborator

    No worries fix is already on the listed PR

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions