Skip to content

Commit 256afac

Browse files
authored
Cards of color pickers (#10)
1 parent e46537f commit 256afac

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

panel/app.py

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
from bokeh.models import PrintfTickFormatter
55
from bokeh.models.widgets.tables import NumberFormatter
6+
import matplotlib as mpl
67
import matplotlib.pyplot as plt
78
import numpy as np
89
import pandas as pd
910
import panel as pn
1011
from panel.layout.gridstack import GridStack
1112

1213
import simdec as sd
14+
from simdec.visualization import colormap_from_single_color
1315

1416

1517
# panel app
@@ -136,8 +138,33 @@ def decomposition(dec_limit, si, inputs, output):
136138
)
137139

138140

139-
def palette(res):
140-
return sd.palette(res.states)
141+
def base_colors(res):
142+
all_colors = sd.palette(res.states)
143+
colors = all_colors[:: res.states[0]]
144+
colors = [mpl.colors.rgb2hex(color, keep_alpha=False) for color in colors]
145+
return colors
146+
147+
148+
def update_colors_select(event):
149+
colors = [color_picker.value for color_picker in color_pickers]
150+
colors_select.param.update(
151+
options=colors,
152+
value=colors,
153+
)
154+
155+
156+
def create_color_pickers(states, colors):
157+
color_picker_list = []
158+
for state, color in zip(states[0][::-1], colors):
159+
color_picker = pn.widgets.ColorPicker(name=state, value=color)
160+
color_picker.param.watch(update_colors_select, "value")
161+
color_picker_list.append(color_picker)
162+
color_pickers[:] = color_picker_list
163+
164+
165+
def palette(res, colors_picked):
166+
cmaps = [colormap_from_single_color(color_picked) for color_picked in colors_picked]
167+
return sd.palette(res.states, cmaps=cmaps)
141168

142169

143170
def n_bins_auto(res):
@@ -255,7 +282,6 @@ def tableau_states(res, states):
255282
interactive_inputs_decomposition,
256283
interactive_output,
257284
)
258-
interactive_palette = pn.bind(palette, interactive_decomposition)
259285

260286
switch_histogram_boxplot = pn.widgets.RadioButtonGroup(
261287
name="Switch histogram - boxplot",
@@ -275,6 +301,29 @@ def tableau_states(res, states):
275301
visible=show_n_bins,
276302
)
277303

304+
interactive_states = pn.bind(
305+
states_from_data, interactive_decomposition, interactive_inputs_decomposition
306+
)
307+
308+
309+
interactive_base_colors = pn.bind(base_colors, interactive_decomposition)
310+
311+
312+
color_pickers = pn.Card(title="Main color for states")
313+
colors_select = pn.widgets.MultiSelect(
314+
value=interactive_base_colors,
315+
options=interactive_base_colors,
316+
name="Colors",
317+
visible=False,
318+
)
319+
320+
dummy_color_pickers_bind = pn.bind(
321+
create_color_pickers, interactive_states, colors_select.param.value, watch=True
322+
)
323+
324+
interactive_palette = pn.bind(
325+
palette, interactive_decomposition, colors_select.param.value
326+
)
278327

279328
interactive_figure = pn.bind(
280329
figure,
@@ -285,10 +334,6 @@ def tableau_states(res, states):
285334
selector_output,
286335
)
287336

288-
289-
interactive_states = pn.bind(
290-
states_from_data, interactive_decomposition, interactive_inputs_decomposition
291-
)
292337
interactive_tableau = pn.bind(
293338
tableau, interactive_decomposition, interactive_states, interactive_palette
294339
)
@@ -312,6 +357,8 @@ def tableau_states(res, states):
312357
pn.pane.Markdown("## Visualization", styles={"color": blue_color}),
313358
switch_histogram_boxplot,
314359
selector_n_bins,
360+
dummy_color_pickers_bind,
361+
color_pickers,
315362
# pn.Row(logout),
316363
max_width=350,
317364
sizing_mode="stretch_width",

src/simdec/visualization.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,11 @@ def palette(
9494
if cmaps is None:
9595
cmaps = [mpl.colormaps[cmap] for cmap in SEQUENTIAL_PALETTES[:n_cmaps]]
9696
else:
97+
cmaps = cmaps[:n_cmaps]
9798
if len(cmaps) != n_cmaps:
9899
raise ValueError(
99-
"Must have the same number of cmaps as the number of first states"
100+
f"Must have the same number of cmaps ({len(cmaps)}) as the "
101+
f"number of first states ({n_cmaps})"
100102
)
101103

102104
colors = []

0 commit comments

Comments
 (0)