Skip to content

Commit a1ff359

Browse files
authored
Use deterministic colors in viz (#621)
Instead of randomly shuffling the list of colors each time, keep a global counter for the index into the list.
1 parent e67fb8a commit a1ff359

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

lonboard/_viz.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from __future__ import annotations
44

55
import json
6-
from random import shuffle
76
from textwrap import dedent
87
from typing import (
98
TYPE_CHECKING,
@@ -84,6 +83,7 @@ def __geo_interface__(self) -> dict: ...
8483
"#FFFF66", # yellow
8584
"#00FFFF", # turquoise
8685
]
86+
COLOR_COUNTER = 0
8787
DEFAULT_POLYGON_LINE_COLOR = [0, 0, 0, 200]
8888

8989

@@ -191,30 +191,33 @@ def viz(
191191
Returns:
192192
widget visualizing the provided data.
193193
"""
194+
global COLOR_COUNTER
194195
color_ordering = COLORS.copy()
195-
shuffle(color_ordering)
196196

197197
if isinstance(data, (list, tuple)):
198198
layers: List[Union[ScatterplotLayer, PathLayer, PolygonLayer]] = []
199199
for i, item in enumerate(data):
200200
ls = create_layers_from_data_input(
201201
item,
202-
_viz_color=color_ordering[i % len(color_ordering)],
202+
_viz_color=color_ordering[(COLOR_COUNTER + i) % len(color_ordering)],
203203
scatterplot_kwargs=scatterplot_kwargs,
204204
path_kwargs=path_kwargs,
205205
polygon_kwargs=polygon_kwargs,
206206
con=con,
207207
)
208208
layers.extend(ls)
209+
210+
COLOR_COUNTER += len(layers)
209211
else:
210212
layers = create_layers_from_data_input(
211213
data,
212-
_viz_color=color_ordering[0],
214+
_viz_color=color_ordering[COLOR_COUNTER % len(color_ordering)],
213215
scatterplot_kwargs=scatterplot_kwargs,
214216
path_kwargs=path_kwargs,
215217
polygon_kwargs=polygon_kwargs,
216218
con=con,
217219
)
220+
COLOR_COUNTER += 1
218221

219222
map_kwargs = {} if not map_kwargs else map_kwargs
220223

0 commit comments

Comments
 (0)