Skip to content

Backport PR #19583 on branch v3.4.x (FIX: check for a set during color conversion) #19615

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"""

import base64
from collections.abc import Sized
from collections.abc import Sized, Sequence
import copy
import functools
import inspect
Expand Down Expand Up @@ -364,11 +364,14 @@ def to_rgba_array(c, alpha=None):

# Quick path if the whole sequence can be directly converted to a numpy
# array in one shot.
lens = {len(cc) if isinstance(cc, (list, tuple)) else -1 for cc in c}
if lens == {3}:
rgba = np.column_stack([c, np.ones(len(c))])
elif lens == {4}:
rgba = np.array(c)
if isinstance(c, Sequence):
lens = {len(cc) if isinstance(cc, (list, tuple)) else -1 for cc in c}
if lens == {3}:
rgba = np.column_stack([c, np.ones(len(c))])
elif lens == {4}:
rgba = np.array(c)
else:
rgba = np.array([to_rgba(cc) for cc in c])
else:
rgba = np.array([to_rgba(cc) for cc in c])

Expand Down
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,18 @@ def test_2d_to_rgba():
assert rgba_1d == rgba_2d


def test_set_dict_to_rgba():
# downstream libraries do this...
# note we can't test this because it is not well-ordered
# so just smoketest:
colors = set([(0, .5, 1), (1, .2, .5), (.4, 1, .2)])
res = mcolors.to_rgba_array(colors)
palette = {"red": (1, 0, 0), "green": (0, 1, 0), "blue": (0, 0, 1)}
res = mcolors.to_rgba_array(palette.values())
exp = np.eye(3)
np.testing.assert_array_almost_equal(res[:, :-1], exp)


def test_norm_deepcopy():
norm = mcolors.LogNorm()
norm.vmin = 0.0002
Expand Down