Skip to content

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

@xyf5432

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 OpenSfM that corresponds to a known unstable API in matplotlib (matplotlib.cm.get_cmap).

Findings

The deprecated cm.get_cmap API is used in the following files:

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 ( 3.9.1) due to community feedback.

The rationale and future removal plan are explicitly documented in the Matplotlib 3.10.0 source code(https://github.com/matplotlib/matplotlib/blob/v3.10.0/lib/matplotlib/cm.py#L246-255):

# 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 3.9.0 installed likely face immediate AttributeError crashes when running these scripts.
  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 (Recommended)
Consider updating the code to use the alternatives suggested in the Matplotlib source code (e.g., matplotlib.colormaps[name] or pyplot.get_cmap()) to ensure long-term stability.

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

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

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