Skip to content

Fix colour arguments under matplotlib >= 3.11 - #119

Open
dgaines2 wants to merge 2 commits into
SMTG-Bham:masterfrom
dgaines2:fix/get-cmap
Open

dgaines2 wants to merge 2 commits into
SMTG-Bham:masterfrom
dgaines2:fix/get-cmap

Conversation

@dgaines2

@dgaines2 dgaines2 commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Fixes #117.

matplotlib 3.11 removed matplotlib.cm.get_cmap. The existing fallback, matplotlib.colormaps[...], raises KeyError/TypeError rather than the ValueError tp uses to fall back to colours, lists and dicts, and it rejects Colormap objects. So on matplotlib >= 3.11 every non-colourmap colour argument fails, and some default calls fail outright: add_dos(ax, data), add_ztdiff, add_pfdiff and tp plot phonons <single band.yaml>.

Changes

  • Swap every colourmap lookup (11 sites in tp/plot/ and tp/cli/cli.py) to matplotlib.pyplot.get_cmap, one of the replacements named in the deprecation warning. It behaves exactly as cm.get_cmap did: Colormap objects are returned unchanged, and anything that isn't a colourmap raises ValueError, so the existing fallback code is untouched.
  • Add tests that pass each colour form (colourmap name, LinearSegmentedColormap and ListedColormap objects, 'skelton', single colour, list, dict) to add_multi, add_wideband, add_heatmap, add_waterfall and add_dos. 31 of them fail without the fix on matplotlib 3.11.
  • test_heatmap.py::HeatmapTest.test_colourmap used the same get_cmap/colormaps fallback to build its input; it now uses plt.get_cmap, which also removes the deprecation warning in the test log.
  • A second commit fixes add_multi(colour='<single colour>'), which raised IndexError for the second dataset on every matplotlib version.

Testing

  • matplotlib 3.11.2, Python 3.12: tests/test_plot passes fully. The remaining failures in the full suite are unrelated and also fail on master: BEOccupationTest under numpy 2.5, and missing data files in tests/test_data/real_data_test.py.
  • matplotlib 3.10.9, Python 3.10 (same setup as CI, python -m unittest): all 175 tests pass.
  • tp plot phonons with one and two band.yaml files, with and without -c, works.

CI only runs Python 3.10, so it can't show the fix on matplotlib 3.11; see #118 for a proposal to test more Python versions.

matplotlib 3.11 removed matplotlib.cm.get_cmap. The fallback,
matplotlib.colormaps[...], raises KeyError or TypeError rather than the
ValueError tp relies on to treat an argument as a colour, list or dict,
and it rejects Colormap objects. So every non-colourmap colour argument
failed, and add_dos, add_ztdiff, add_pfdiff and `tp plot phonons` with a
single band.yaml failed even with default arguments.

Use matplotlib.pyplot.get_cmap, one of the replacements named in the
deprecation warning. It behaves as cm.get_cmap did: Colormap objects are
returned unchanged and anything that is not a colourmap raises
ValueError. Add tests for each colour form.
A single colour string gave a one-element colour list, so plotting any
dataset after the first raised IndexError, on every matplotlib version.
Repeat the colour for every dataset.

This branch has not been deployed

No deployments
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.

Colour arguments fail with matplotlib >= 3.11

1 participant