8000 [Bug]: LinearSegmentedColormap.from_list cannot process list with two colors · Issue #27126 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
[Bug]: LinearSegmentedColormap.from_list cannot process list with two colors #27126
Closed
@AgilentGCMS

Description

@AgilentGCMS

Bug summary

I use LinearSegmentedColormap.from_list to create custom colormaps. A very simple one, which just assigns two RGB values to the two end points, does not work any more after update to matplotlib 3.8.0.

Code for reproduction

from matplotlib import colors
val_col =  [(0.0, (0.95, 0.0, 0.0)), (1.0, (0.95, 0.95, 0.95))]
cm = colors.LinearSegmentedColormap.from_list('test', val_col)

Actual outcome

File ~/packages/macports/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/matplotlib/colors.py:1067, in LinearSegmentedColormap.from_list(name, colors, N, gamma)
   1063     vals = np.linspace(0, 1, len(colors))
   1065 r, g, b, a = to_rgba_array(colors).T
   1066 cdict = {
-> 1067     "red": np.column_stack([vals, r, r]),
   1068     "green": np.column_stack([vals, g, g]),
   1069     "blue": np.column_stack([vals, b, b]),
   1070     "alpha": np.column_stack([vals, a, a]),
   1071 }
   1073 return LinearSegmentedColormap(name, cdict, N, gamma)

File ~/packages/macports/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/numpy/lib/shape_base.py:652, in column_stack(tup)
    650         arr = array(arr, copy=False, subok=True, ndmin=2).T
    651     arrays.append(arr)
--> 652 return _nx.concatenate(arrays, 1)

ValueError: all the input array dimensions except for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 2 and the array at index 1 has size 3

Expected outcome

This should construct a new colormap going smoothly from red to white. This used to work until I recently updated matplotlib to 3.8.0.

Additional information

The issue seems to be that the routine to_rgba_array takes a length-2 object and returns r, g and b that are all length 3. Specifically, the line

r, g, b, a = to_rgba_array(colors).T

takes colors, which is ((0.95, 0.0, 0.0), (0.95, 0.95, 0.95)), and returns r as [0.95 0.95 0.95], and so on. It's obvious that np.column_stack([vals, r, r]) should fail in that case, because vals is length 2, and r is length 3. This used to work in an earlier version of matplotlib, so I suppose to_rgba_array has changed recently.

Operating system

OS/X

Matplotlib Version

3.8.0

Matplotlib Backend

MacOSX

Python version

3.11.6

Jupyter version

No response

Installation

Linux package manager

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0