Skip to content

Potential compatibility issues with matplotlib.cm.get_cmap (Incompatible with Matplotlib 3.9.0) #1031

Description

@xyf5432

Context

We are currently analyzing historical GitHub issues to identify potential compatibility regressions in open-source projects. During this process, we noticed a usage pattern in MockingBird that corresponds to a known unstable API in matplotlib (matplotlib.cm.get_cmap).

Findings

The deprecated cm.get_cmap API is used in models/encoder/inference.py:

# models/encoder/inference.py
cmap = cm.get_cmap()  # <--- This line triggers AttributeError on specific versions
mappable = ax.imshow(embed, cmap=cmap)

Evidence & History

This API has a history of instability. It was briefly removed in Matplotlib in 3.9.0, before being restored in patch releases due to community feedback.

The rationale and future removal plan are explicitly documented in the Matplotlib 3.10.0 source code:

# This is an exact copy of pyplot.get_cmap(). It was removed in 3.9, but apparently
# caused more user trouble than expected. Re-added for 3.9.1 and extended the
# deprecation period for two additional minor releases.
@_api.deprecated(
    '3.7',
    removal='3.11',
    alternative="``matplotlib.colormaps[name]`` or ``matplotlib.colormaps.get_cmap()``"
                " or ``pyplot.get_cmap()``"
)
def get_cmap(name=None, lut=None):
    # ...

Potential Impact

  1. Suspected Crashes: Users whose environments happen to have matplotlib==3.9.0 installed likely face immediate AttributeError: module 'matplotlib.cm' has no attribute 'get_cmap' crashes.
  2. Future Incompatibility: As noted in the source code above, the API is scheduled for permanent removal in version 3.11.

Experimental Evidence

Our local testing confirms that matplotlib.cm.get_cmap was physically removed in Matplotlib 3.9.0, leading to an immediate crash.

Verification on Matplotlib 3.9.0:

>>> import matplotlib
>>> print(matplotlib.__version__)
3.9.0
>>> from matplotlib import cm
>>> cmap = cm.get_cmap(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'matplotlib.cm' has no attribute 'get_cmap'

Suggestions

Option 1: Update API
Consider updating the code to use one of the official alternatives suggested in the Matplotlib source code:

  • matplotlib.colormaps[name]
  • matplotlib.colormaps.get_cmap()
  • pyplot.get_cmap()

Option 2: Restrict Dependency Version
If a code update is not immediate, you might consider temporarily restricting the dependency versions to avoid the unstable releases:

dependencies = [
    "matplotlib!=3.9.0,<3.11",
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions