Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: matplotlib.colors.LinearSegmentedColormap.from_list doesn't work with hex representation of colors #27029

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
jose-vines-toteat opened this issue Oct 8, 2023 · 5 comments

Comments

@jose-vines-toteat
Copy link

Bug summary

For some reason colors.LinearSegmentedColormap.from_list stopped working with colors such as '#E3DBCC', when in previous versions it used to work.

It raises an error in to_rgba_array(colors), but when actually calling colors.to_rgba_array(['#E3DBCC']) it works just fine.

Code for reproduction

import matplotlib.colors as colors
start = '#E3DBCC'
end = '#FF4B33'

color_list = [(0, start), (1, end)]
my_cmap = colors.LinearSegmentedColormap.from_list('my_cmap', color_list)

Actual outcome

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[12], line 6
      3 end = '#FF4B33'
      5 color_list = [(0, start), (1, end)]
----> 6 my_cmap = colors.LinearSegmentedColormap.from_list('my_cmap', color_list)

File ~/mambaforge/envs/tf/lib/python3.9/site-packages/matplotlib/colors.py:1065, in LinearSegmentedColormap.from_list(name, colors, N, gamma)
   1062 else:
   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 ~/mambaforge/envs/tf/lib/python3.9/site-packages/matplotlib/colors.py:489, in to_rgba_array(c, alpha)
    487         raise e
    488 if isinstance(c, str):
--> 489     raise ValueError(f"{c!r} is not a valid color value.")
    491 if len(c) == 0:
    492     return np.zeros((0, 4), float)

ValueError: '#E3DBCC' is not a valid color value.

Expected outcome

I expect the LinearSegmentedColormap to be created successfully.

Additional information

No response

Operating system

OS X 13.1

Matplotlib Version

3.8.0

Matplotlib Backend

'module://matplotlib_inline.backend_inline'

Python version

3.9

Jupyter version

3.6.1

Installation

conda

@jklymak
Copy link
Member

jklymak commented Oct 8, 2023

Do you know what version this used to work with?

@jose-vines-toteat
Copy link
Author

jose-vines-toteat commented Oct 8, 2023

It worked last in 3.7.0. I think the problem is colors.to_rgba_array when c is a tuple, particularly line 438

if isinstance(c, tuple) and len(c) == 2:
    if alpha is None:
        c, alpha = c

In this case with my color list c = ('#E3DBCC', '#FF4B33') gets transformed into just the first value, and the second hex is treated like the alpha channel value. Thus eventually it reaches the if isinstance(c, str) bit and raises the error.

Indeed, if I add a third color, the problem disappears.

@jklymak
Copy link
Member

jklymak commented Oct 8, 2023

This was fixed in #26952. However, I'm not sure #26952 has a test of hex, so maybe there should be another test added.

@rcomer
Copy link
Member

rcomer commented Oct 8, 2023

The original issue #26949 also used hexes but the failure path was the same regardless of whether I tried the hex representation or the abbreviated (“k”, “w”) colour representation. So I do not think we need an extra test to cover the hex version of this bug, but obviously I’m biased as it was my PR 🙂

@jklymak jklymak closed this as not planned Won't fix, can't repro, duplicate, stale Oct 8, 2023
@jklymak
Copy link
Member

jklymak commented Oct 8, 2023

Thanks for the report. This will be fixed in the next bug release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants