Skip to content

ImportError with Matplotlib 3.11: matplotlib.cm.get_cmap was removed #12

Description

@harveybi

Description

SUITPy 2.1.1 cannot be imported when used with Matplotlib 3.11.

SUITPy currently declares the following dependency in setup.py:

matplotlib>=3.5.0

Therefore, pip may install Matplotlib 3.11. However, SUITPy/flatmap.py still imports get_cmap from matplotlib.cm:

from matplotlib.cm import ScalarMappable, get_cmap

matplotlib.cm.get_cmap was removed in Matplotlib 3.11, causing import SUITPy to fail.

Environment

  • OS: Linux
  • Python: 3.12.14
  • SUITPy: 2.1.1
  • Matplotlib: 3.11.1

Steps to reproduce

Install the affected versions:

python -m pip install "SUITPy==2.1.1" "matplotlib==3.11.1"

Then try to import SUITPy:

python -c "import SUITPy"

Actual behavior

The import fails with:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File ".../site-packages/SUITPy/__init__.py", line 10, in <module>
    from .flatmap import ...
  File ".../site-packages/SUITPy/flatmap.py", line 20, in <module>
    from matplotlib.cm import ScalarMappable, get_cmap
ImportError: cannot import name 'get_cmap' from 'matplotlib.cm'

This prevents all SUITPy functionality from being used, even when the flatmap plotting functions are not needed.

Expected behavior

import SUITPy should succeed with dependency versions permitted by SUITPy's package metadata.

If Matplotlib 3.11 is not yet supported, the dependency metadata could temporarily specify:

matplotlib>=3.5.0,<3.11

Cause

Matplotlib 3.11 removed matplotlib.cm.get_cmap. The recommended alternatives are matplotlib.colormaps.get_cmap and matplotlib.pyplot.get_cmap.

Matplotlib documents this removal here:

https://matplotlib.org/stable/api/prev_api_changes/api_changes_3.11.0.html#matplotlib-cm-get-cmap

Possible fix

In the current flatmap.py, the imported get_cmap appears to be unused. Colormap lookup is already performed through:

plt.get_cmap(...)

Therefore, the import could likely be changed from:

from matplotlib.cm import ScalarMappable, get_cmap

to:

from matplotlib.cm import ScalarMappable

If get_cmap is required elsewhere in the future, it could instead be obtained through the supported API:

from matplotlib import colormaps

cmap = colormaps.get_cmap(name)

Suggested resolution

Would it be possible to:

  1. Remove the unused get_cmap import or replace it with the supported API;
  2. Add a test that imports SUITPy using the latest supported Matplotlib version; and
  3. Publish the fix in a patch release?

Thank you for maintaining SUITPy.

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