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

Skip to content

[Bug]: to_rgba_array("none") returns empty array #28475

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

Open
story645 opened this issue Jun 27, 2024 · 3 comments
Open

[Bug]: to_rgba_array("none") returns empty array #28475

story645 opened this issue Jun 27, 2024 · 3 comments

Comments

@story645
Copy link
Member

story645 commented Jun 27, 2024

Bug summary

mcolors.to_rgba_array("none") returns an empty array while mcolors.to_rgba("none") and mcolors.to_rgba_array(["none"]) return (0,0,0,0) (which is the documented value of None) and this leads to inconsistencies when trying to use mcolors.same_color

Actual outcome

>>> import matplotlib.colors as mcolors
>>> mcolors.to_rgba("none")
(0.0, 0.0, 0.0, 0.0)
>>> mcolors.to_rgba_array("none") 
array([], shape=(0, 4), dtype=float64)
>>> mcolors.to_rgba_array(["none"])
array([[0., 0., 0., 0.]])
>>> mcolors.same_color("none", mcolors.to_rgba("none"))
False
>>> mcolors.same_color(["none"], mcolors.to_rgba("none")) 
True

Expected outcome

>>> import matplotlib.colors as mcolors
>>> mcolors.to_rgba("none")
(0.0, 0.0, 0.0, 0.0)
>>> mcolors.to_rgba_array("none") 
array([[0., 0., 0., 0.]])
>>> mcolors.to_rgba_array(["none"])
array([[0., 0., 0., 0.]])
>>> mcolors.same_color("none", mcolors.to_rgba("none"))
True
>>> mcolors.same_color(["none"], mcolors.to_rgba("none")) 
True

Additional information

I tried the quick fixes of changing the return here to array([[0., 0., 0., 0.]]) and np.array([to_rgba('none')], float)

if cbook._str_lower_equal(c, "none"):
return np.zeros((0, 4), float)

That yielded a ton of test failures, mostly around colorbars/contours, hatches, and but roughly the diffs all looked something like this:

contourf_extend_patches (actual, expected)
image

colorbar_two_slope
image

Also I realize this could be not worth fixing and a quick fix is to cast none to a list in same_color

@story645
Copy link
Member Author

story645 commented Aug 12, 2024

Someone at scipy tracked this down to being an issue w/ eager conversion to color that relies on 'none'/empty array for if statements to preserve the correct alpha values. xref #28710

@rcomer
Copy link
Member

rcomer commented Mar 9, 2025

The existing behaviour is clearly deliberate but I agree with @story645 about what I expected to happen. Is it worth adding a boolean parameter so you could choose between the two behaviours?

@story645
Copy link
Member Author

story645 commented Mar 9, 2025

Is it worth adding a boolean parameter so you could choose between the two behaviours?

I've been thinking of a sort of true constant for none, but flagging would probably do the same trick - what I'm thinking is that it could be really useful to know which parts of the codebase need the empty behavior.

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

2 participants