Skip to content

fix: ensure pyplot set_cmap handles registered keys (swev-id: matplotlib__matplotlib-25479)#60

Open
casey-brooks wants to merge 1 commit intomatplotlib__matplotlib-25479from
fix/set_cmap-rcparams-registered-key-279
Open

fix: ensure pyplot set_cmap handles registered keys (swev-id: matplotlib__matplotlib-25479)#60
casey-brooks wants to merge 1 commit intomatplotlib__matplotlib-25479from
fix/set_cmap-rcparams-registered-key-279

Conversation

@casey-brooks
Copy link

Summary

  • ensure pyplot.set_cmap stores the passed registry key when provided a string
  • keep colormap-object inputs writing the object's intrinsic name
  • add regression coverage for rcParams behavior with both string and object inputs

Fixes #58

Testing

  • MPLBACKEND=Agg PYTHONPATH=/workspace/matplotlib/lib LD_LIBRARY_PATH=/root/.nix-profile/lib:/nix/store/qipd93x9gjyiygqk673rd2ssnf8y7jj0-gcc-14.3.0-lib/lib:/nix/store/f8w1i7yisixb9hivzbk0l4ixmf67fjqr-gcc-14.3.0-libgcc/lib .venv/bin/pytest lib/matplotlib/tests/test_pyplot.py -k 'set_cmap_'
  • .venv/bin/flake8 lib/matplotlib/pyplot.py lib/matplotlib/tests/test_pyplot.py

Reproduction

Pre-fix script:

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

lsc = LinearSegmentedColormap.from_list('some_cmap_name', ['black', 'white'])
mpl.colormaps.register(lsc, name='my_cmap_name', force=True)
plt.set_cmap('my_cmap_name')
plt.imshow(np.arange(4).reshape(2, 2))

Observed failure (excerpt):

ValueError: 'some_cmap_name' is not a valid value for cmap; supported values are ... 'my_cmap_name', ...
  File ".../matplotlib/cm.py", line 722, in _ensure_cmap
    _api.check_in_list(sorted(_colormaps), cmap=cmap_name)

Post-fix confirmation:

imshow succeeded; rc image.cmap = my_cmap_name

@casey-brooks casey-brooks requested a review from a team December 25, 2025 12:42
@casey-brooks
Copy link
Author

Test & Lint Summary

  • MPLBACKEND=Agg PYTHONPATH=/workspace/matplotlib/lib LD_LIBRARY_PATH=/root/.nix-profile/lib:/nix/store/qipd93x9gjyiygqk673rd2ssnf8y7jj0-gcc-14.3.0-lib/lib:/nix/store/f8w1i7yisixb9hivzbk0l4ixmf67fjqr-gcc-14.3.0-libgcc/lib .venv/bin/pytest lib/matplotlib/tests/test_pyplot.py -k 'set_cmap_' → 2 passed, 26 deselected
  • .venv/bin/flake8 lib/matplotlib/pyplot.py lib/matplotlib/tests/test_pyplot.py → no issues

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. The change ensures string inputs write the registered key into rcParams while colormap objects continue to use their intrinsic names, and the new regression tests cover both code paths.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants