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

Skip to content

[Bug]: colors.LinearSegmentedColormap.from_list does not take two tuples in 3.8.0 #26949

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
dave-shirley-faraday opened this issue Sep 28, 2023 · 3 comments · Fixed by #26952
Closed

Comments

@dave-shirley-faraday
Copy link

dave-shirley-faraday commented Sep 28, 2023

Bug summary

Incrementing from v3.7 to v3.8 the option to specify a start and end value is no longer functioning when two values are specified. It still works when three are specified.

## This works:
matplotlib.colors.LinearSegmentedColormap.from_list("myMap", ["#000000", "#FFFFFF"])
## This does not:
matplotlib.colors.LinearSegmentedColormap.from_list("myMap", [(0,"#000000"), (1,"#FFFFFF")])

Code for reproduction

matplotlib.colors.LinearSegmentedColormap.from_list("myMap", [(0,"#000000"), (1,"#FFFFFF")])

Actual outcome

Traceback (most recent call last):
File "", line 1, in
File "/Users/dave/.local/share/virtualenvs/projects-t0xU6-ce/lib/python3.10/site-packages/matplotlib/colors.py", line 1065, in from_list
colors : array-like of colors or array-like of (value, color)
File "/Users/dave/.local/share/virtualenvs/projects-t0xU6-ce/lib/python3.10/site-packages/matplotlib/colors.py", line 489, in to_rgba_array
pass
ValueError: '#000000' is not a valid color value.

Expected outcome

<matplotlib.colors.LinearSegmentedColormap object at 0x101e7f520>

Additional information

Worked in 3.7

The issue I think is related to zipped values being passed to r, g, b, a = to_rgba_array(colors).T. A tuple gets passed in rather than a list.

## tuple len 2 fails
>>> matplotlib.colors.to_rgba_array(("#000000", "#FFFFFF"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/dave/.local/share/virtualenvs/model_train-t0xU6-ce/lib/python3.10/site-packages/matplotlib/colors.py", line 489, in to_rgba_array
    pass
ValueError: '#000000' is not a valid color value.
## list works
>>> matplotlib.colors.to_rgba_array(["#000000", "#FFFFFF"])
array([[0., 0., 0., 1.],
       [1., 1., 1., 1.]])

Changing r, g, b, a = to_rgba_array(colors).T in colors.py:LinearSegmentedColormap.from_list to r, g, b, a = to_rgba_array(list(colors)).T or similar may fix.

Operating system

OS/X

Matplotlib Version

3.8.0

Matplotlib Backend

MacOSX

Python version

Python 3.10.9

Jupyter version

No response

Installation

None

@rcomer
Copy link
Member

rcomer commented Sep 28, 2023

Thanks for the report and the debugging notes @dave-shirley-faraday. This bisects to #24691.

@rcomer rcomer added this to the v3.8.1 milestone Sep 28, 2023
@rcomer
Copy link
Member

rcomer commented Sep 28, 2023

This seems to depend on whether to_rgba gets a list or a tuple:

In [10]: to_rgba_array(["#000000", "#FFFFFF"])
Out[10]: 
array([[0., 0., 0., 1.],
       [1., 1., 1., 1.]])

In [11]: to_rgba_array(("#000000", "#FFFFFF"))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[11], line 1
----> 1 to_rgba_array(("#000000", "#FFFFFF"))

File ~/git_stuff/matplotlib/lib/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: '#000000' is not a valid color value.

Edit: sorry, I see you already said that!

@dave-shirley-faraday
Copy link
Author

dave-shirley-faraday commented Sep 28, 2023

Yes I think with the new parameterization of to_rgba the tuple ("#000000", "#FFFFFF") is now treated as (c, alpha) rather than (c, c), where hex values are not valid color values (it expects 'red' or similar). Perhaps detecting the color type (eg checking for a leading #) in to_rgba might fix.

[edit] Yup, looks like your PR should address

facundolucianna added a commit to FIUBA-Posgrado-Inteligencia-Artificial/intro_ia that referenced this issue May 11, 2024
facundolucianna added a commit to FIUBA-Posgrado-Inteligencia-Artificial/aprMaqI_CEIA that referenced this issue May 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants