You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: LinearSegmentedColormap.from_list cannot process list with two colors #27126
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.
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.
Activity
ksunden commentedon 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.
AgilentGCMS commentedon Oct 17, 2023
@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 commentedon Oct 17, 2023
@ksunden Never mind, I figured it out from #26952
rcomer commentedon Oct 17, 2023
If you are in a hurry, it is also possible to install the development version with
piphttps://matplotlib.org/stable/users/installing/index.html#install-a-nightly-build