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

Skip to content
Merged
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
31 changes: 20 additions & 11 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,20 @@ def to_rgba_array(c, alpha=None):

if len(c) == 0:
return np.zeros((0, 4), float)

# 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)
else:
if np.iterable(alpha):
return np.array([to_rgba(cc, aa) for cc, aa in zip(c, alpha)])
else:
return np.array([to_rgba(cc, alpha) for cc in c])
rgba = np.array([to_rgba(cc) for cc in c])

if alpha is not None:
rgba[:, 3] = alpha
return rgba


def to_rgb(c):
Expand Down Expand Up @@ -914,13 +923,13 @@ def from_list(name, colors, N=256, gamma=1.0):
else:
vals = np.linspace(0, 1, len(colors))

cdict = dict(red=[], green=[], blue=[], alpha=[])
for val, color in zip(vals, colors):
r, g, b, a = to_rgba(color)
cdict['red'].append((val, r, r))
cdict['green'].append((val, g, g))
cdict['blue'].append((val, b, b))
cdict['alpha'].append((val, a, a))
r, g, b, a = to_rgba_array(colors).T
cdict = {
"red": np.column_stack([vals, r, r]),
"green": np.column_stack([vals, g, g]),
"blue": np.column_stack([vals, b, b]),
"alpha": np.column_stack([vals, a, a]),
}

return LinearSegmentedColormap(name, cdict, N, gamma)

Expand Down