Skip to content

[Bug]: LinearSegmentedColormap.from_list cannot process list with two colors #27126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
AgilentGCMS opened this issue Oct 17, 2023 · 4 comments
Closed
Milestone

Comments

@AgilentGCMS
Copy link

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

@ksunden
Copy link
Member

ksunden commented Oct 17, 2023

This was fixed by #26952, and will be released with v3.8.1, which is due out once we clear up just a few more bugs.

@ksunden ksunden closed this as completed Oct 17, 2023
@ksunden ksunden added this to the v3.8.1 milestone Oct 17, 2023
@AgilentGCMS
Copy link
Author

@ksunden Good to know! Is there a diff somewhere I can apply on my own, if I can't wait for a release of 3.8.1?

@AgilentGCMS
Copy link
Author

@ksunden Never mind, I figured it out from #26952

@rcomer
Copy link
Member

rcomer commented Oct 17, 2023

If you are in a hurry, it is also possible to install the development version with pip
https://matplotlib.org/stable/users/installing/index.html#install-a-nightly-build

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

No branches or pull requests

3 participants