matplotlib / matplotlib/matplotlib

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

Open
#28,475 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: color/color & colormaps topic: testing
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)
![image](https://github.com/matplotlib/matplotlib/assets/1300499/865eaa8a-2293-4b73-bf21-e8b9f95e3831)

colorbar_two_slope
![image](https://github.com/matplotlib/matplotlib/assets/1300499/cbce73cd-e4f5-4caf-9e17-b8881dc01a96)

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.