matplotlib / matplotlib/matplotlib
[Bug]: to_rgba_array("none") returns empty array
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 23.2k
- Forks
- 8.5k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 66
Description
### 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](https://matplotlib.org/devdocs/api/_as_gen/matplotlib.colors.same_color.html#matplotlib.colors.same_color)
### Actual outcome
```pycon
>>> 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
```pycon
>>> 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)`
https://github.com/matplotlib/matplotlib/blob/d347c3227f8de8a99aa327390fee619310452a96/lib/matplotlib/colors.py#L482-L483
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)

colorbar_two_slope

Also I realize this could be not worth fixing and a quick fix is to cast `none` to a list in `same_color`
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the documented examples and inspect lib/matplotlib/colors.py around the linked lines 482-483. Check the effects of a proposed behavior change on the reported contourf_extend_patches and colorbar_two_slope tests, as well as the mentioned colorbar, contours, and hatches cases; done means consistent scalar and list handling without those regressions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data-visualization
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100