Skip to content

Commit

Permalink
Use deterministic colors in viz (#621)
Browse files Browse the repository at this point in the history
Instead of randomly shuffling the list of colors each time, keep a
global counter for the index into the list.
  • Loading branch information
kylebarron authored Sep 3, 2024
1 parent e67fb8a commit a1ff359
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lonboard/_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import annotations

import json
from random import shuffle
from textwrap import dedent
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -84,6 +83,7 @@ def __geo_interface__(self) -> dict: ...
"#FFFF66", # yellow
"#00FFFF", # turquoise
]
COLOR_COUNTER = 0
DEFAULT_POLYGON_LINE_COLOR = [0, 0, 0, 200]


Expand Down Expand Up @@ -191,30 +191,33 @@ def viz(
Returns:
widget visualizing the provided data.
"""
global COLOR_COUNTER
color_ordering = COLORS.copy()
shuffle(color_ordering)

if isinstance(data, (list, tuple)):
layers: List[Union[ScatterplotLayer, PathLayer, PolygonLayer]] = []
for i, item in enumerate(data):
ls = create_layers_from_data_input(
item,
_viz_color=color_ordering[i % len(color_ordering)],
_viz_color=color_ordering[(COLOR_COUNTER + i) % len(color_ordering)],
scatterplot_kwargs=scatterplot_kwargs,
path_kwargs=path_kwargs,
polygon_kwargs=polygon_kwargs,
con=con,
)
layers.extend(ls)

COLOR_COUNTER += len(layers)
else:
layers = create_layers_from_data_input(
data,
_viz_color=color_ordering[0],
_viz_color=color_ordering[COLOR_COUNTER % len(color_ordering)],
scatterplot_kwargs=scatterplot_kwargs,
path_kwargs=path_kwargs,
polygon_kwargs=polygon_kwargs,
con=con,
)
COLOR_COUNTER += 1

map_kwargs = {} if not map_kwargs else map_kwargs

Expand Down

0 comments on commit a1ff359

Please sign in to comment.