Skip to content

Commit 48f6f14

Browse files
authored
colors="random" for line, line collection and scatter (#175)
1 parent 4e53ea7 commit 48f6f14

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

fastplotlib/graphics/features/_colors.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22

33
from ._base import GraphicFeature, GraphicFeatureIndexable, cleanup_slice, FeatureEvent
4-
from ...utils import make_colors, get_cmap_texture
4+
from ...utils import make_colors, get_cmap_texture, make_pygfx_colors
55
from pygfx import Color
66

77

@@ -80,10 +80,15 @@ def __init__(self, parent, colors, n_colors: int, alpha: float = 1.0, collection
8080
f"Valid iterable color arguments must be a `tuple` or `list` representing RGBA values or "
8181
f"an iterable of `str` with the same length as the number of datapoints."
8282
)
83+
elif isinstance(colors, str):
84+
if colors == "random":
85+
data = np.random.rand(n_colors, 4)
86+
data[:, -1] = alpha
87+
else:
88+
data = make_pygfx_colors(colors, n_colors)
8389
else:
8490
# assume it's a single color, use pygfx.Color to parse it
85-
c = Color(colors)
86-
data = np.repeat(np.array([c]), n_colors, axis=0)
91+
data = make_pygfx_colors(colors, n_colors)
8792

8893
if alpha != 1.0:
8994
data[:, -1] = alpha

fastplotlib/graphics/line_collection.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def __init__(
2727
z_position: Union[List[float], float] = None,
2828
thickness: Union[float, List[float]] = 2.0,
2929
colors: Union[List[np.ndarray], np.ndarray] = "w",
30+
alpha: float = 1.0,
3031
cmap: Union[List[str], str] = None,
3132
name: str = None,
3233
*args,
@@ -131,33 +132,44 @@ def __init__(
131132
"with the same length as the data")
132133
else:
133134
if isinstance(colors, np.ndarray):
135+
# single color for all lines in the collection as RGBA
134136
if colors.shape == (4,):
135137
single_color = True
136138

139+
# colors specified for each line as array of shape [n_lines, RGBA]
137140
elif colors.shape == (len(data), 4):
138141
single_color = False
139142

140143
else:
141144
raise ValueError(
142-
"numpy array colors argument must be of shape (4,) or (len(data), 4)"
145+
f"numpy array colors argument must be of shape (4,) or (n_lines, 4)."
146+
f"You have pass the following shape: {colors.shape}"
143147
)
144148

145149
elif isinstance(colors, str):
146-
single_color = True
147-
colors = pygfx.Color(colors)
150+
if colors == "random":
151+
colors = np.random.rand(len(data), 4)
152+
colors[:, -1] = alpha
153+
single_color = False
154+
else:
155+
# parse string color
156+
single_color = True
157+
colors = pygfx.Color(colors)
148158

149159
elif isinstance(colors, (tuple, list)):
150160
if len(colors) == 4:
161+
# single color specified as (R, G, B, A) tuple or list
151162
if all([isinstance(c, (float, int)) for c in colors]):
152163
single_color = True
153164

154165
elif len(colors) == len(data):
166+
# colors passed as list/tuple of colors, such as list of string
155167
single_color = False
156168

157169
else:
158170
raise ValueError(
159171
"tuple or list colors argument must be a single color represented as [R, G, B, A], "
160-
"or must be a str of tuple/list with the same length as the data"
172+
"or must be a tuple/list of colors represented by a string with the same length as the data"
161173
)
162174

163175
self._set_world_object(pygfx.Group())

fastplotlib/utils/functions.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import numpy as np
2-
from pygfx import Texture
2+
from pygfx import Texture, Color
33
from collections import OrderedDict
44
from typing import *
55
from pathlib import Path
@@ -147,3 +147,10 @@ def quick_min_max(data: np.ndarray) -> Tuple[float, float]:
147147
data = data[tuple(sl)]
148148

149149
return float(np.nanmin(data)), float(np.nanmax(data))
150+
151+
152+
def make_pygfx_colors(colors, n_colors):
153+
"""parse and make colors array using pyfx.Color"""
154+
c = Color(colors)
155+
colors_array = np.repeat(np.array([c]), n_colors, axis=0)
156+
return colors_array

0 commit comments

Comments
 (0)