Hey @p-himik I’m sorry to bring this up again as you’re probably sick of me and I’ve already made enough of a fool of myself. I’m trying to adapt the logic used to create these lines with ColumnDataSource and I can’t figure out where I’m going wrong.
I have a heatmap you might recognize and the values are controlled by a slider, and I’d like this same controlled data to be shown as lines. I think the issue might be caused by source = ColumnDataSource(column)
- can CDS be supplied with whatever you’re trying to create a group / subset of in this way?
from pandas import *
from bokeh.io import show
from bokeh.layouts import column
from bokeh.models import LinearColorMapper, CustomJS, Slider, ColumnDataSource
from bokeh.palettes import Viridis256
from bokeh.plotting import figure
df = DataFrame({'attribute': ['Y', 'Y', 'Y', 'Y', 'Z', 'Z', 'Z', 'Z']
, 'period': [1, 2, 3, 4, 1, 2, 3, 4]
, '1': [1, 37, 44, 13, 41, 51, 18, 14]
, '2': [10, 3, 44, 53, 20, 9, 18, 14]
, '3': [80, 37, 22, 13, 13, 44, 18, 14]})
df['period'] = df['period'].astype(str)
periods = df.period.unique().tolist()
attributes = df.attribute.unique().tolist()
selectable_columns = ['1', '2', '3']
source=ColumnDataSource(df)
active = 1
values_select = Slider(title="Values", start=1, end=3, step=1, value=active)
color_mapper = LinearColorMapper(palette=Viridis256, low=df[str(active)].min(), high=df[str(active)].max())
heatmap_fig = figure(x_range=periods, y_range=attributes)
renderer = heatmap_fig.rect(x="period", y="attribute", width=1, height=1, line_color=None, source=source, name=str(active),
fill_color={'field': str(active), 'transform': color_mapper})
line_fig = figure()
for column in selectable_columns:
source = ColumnDataSource(column)
line_fig.line(x='period'
, y='attribute'
, legend_label=str(column)
, source=source)
values_select.js_on_change('value', CustomJS(args=dict(renderer=renderer, heatmap_fig=heatmap_fig, line_fig=line_fig), code="""\
const active = cb_obj.value.toString();
const data = renderer.data_source.data[active];
renderer.name = active;
const {transform} = renderer.glyph.fill_color;
renderer.glyph.fill_color = {field: cb_obj.value, transform: transform};
heatmap_fig.reset.emit()
line_fig.reset.emit()
"""))
show(column(values_select, heatmap_fig, line_fig))
The error says it expects a dict or pandas.df … but how else could I supply the data?