Skip to content

[Bug]: Regression in 3.11.0 with LinearSegmentedColormap #31939

Description

@peterjc

Bug summary

Minimal test case using LinearSegmentedColormap extracted from https://github.com/pyani-plus/pyani-plus/blob/v1.0.1/pyani_plus/plot_run.py which worked on macOS and Linux with eg matplotlib==3.10.8 but breaks on matplotlib-3.11.0 with:

ValueError: the values passed in the (value, color) pairs must increase monotonically from 0 to 1.

The release notes API changes do not mention this, https://matplotlib.org/stable/api/prev_api_changes/api_changes_3.11.0.html

Code for reproduction

from matplotlib import colormaps, __version__
from matplotlib.colors import LinearSegmentedColormap

print(f"matplotlib {__version__}")

GREY = (0.7, 0.7, 0.7)
DULL_BLUE = (0.137, 0.412, 0.737)
WHITE = (1.0, 1.0, 1.0)
DULL_RED = (0.659, 0.216, 0.231)

colormaps.register(
    LinearSegmentedColormap.from_list(
        "spbnd_BuRd",  # species boundary - blue to red
        (
            (0.00, GREY),  # 0% grey
            (0.80, GREY),  # 80% grey (not meaningful)
            (0.80, DULL_BLUE),  # 80% blue
            (0.95, WHITE),  # 95% white (species boundary)
            (1.00, DULL_RED),  # 100% red
        ),
    )
)

print("Done")

Actual outcome

It now fails:

❯ pip install matplotlib==3.11.0
Collecting matplotlib==3.11.0
...
Successfully installed matplotlib-3.11.0

❯ python test.py
matplotlib 3.11.0
Traceback (most recent call last):
  File "/Users/peterjc/miniforge3/envs/pyani-plus_py312/lib/python3.12/site-packages/matplotlib/colors.py", line 1218, in from_list
    r, g, b, a = to_rgba_array(colors).T
                 ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/peterjc/miniforge3/envs/pyani-plus_py312/lib/python3.12/site-packages/matplotlib/colors.py", line 532, in to_rgba_array
    rgba = np.array([to_rgba(cc) for cc in c])
                     ^^^^^^^^^^^
  File "/Users/peterjc/miniforge3/envs/pyani-plus_py312/lib/python3.12/site-packages/matplotlib/colors.py", line 345, in to_rgba
    rgba = _to_rgba_no_colorcycle(c, alpha)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/peterjc/miniforge3/envs/pyani-plus_py312/lib/python3.12/site-packages/matplotlib/colors.py", line 364, in _to_rgba_no_colorcycle
    if alpha is not None and not 0 <= alpha <= 1:
                                 ^^^^^^^^^^^^^^^
TypeError: '<=' not supported between instances of 'int' and 'tuple'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/private/tmp/test.py", line 12, in <module>
    LinearSegmentedColormap.from_list(
  File "/Users/peterjc/miniforge3/envs/pyani-plus_py312/lib/python3.12/site-packages/matplotlib/colors.py", line 1229, in from_list
    raise ValueError(
ValueError: the values passed in the (value, color) pairs must increase monotonically from 0 to 1.

Expected outcome

This used to work, eg

❯ pip install matplotlib==3.10.8
Collecting matplotlib==3.10.8
...
Successfully installed matplotlib-3.10.8

❯ python test.py
matplotlib 3.10.8
Done

Additional information

No response

Operating system

No response

Matplotlib Version

3.11.0

Matplotlib Backend

macosx (or Linux tested via CI)

Python version

3.12.10 (and Python 3.11 and 3.13 via CI)

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions