I am new in Bokeh and I am trying to change the colors of the points of a scatterplot depending on the option selected in a checkbox. If the user selects ‘all’ then all the points should be black, else if the user selects ‘group’ then the points should be colored by group (group A = green, group B = blue). I have attached a tentative script below. So far nothing happens when the checkbox is triggered because Im having trouble with the customjs callback.
Any help appreciated,
# Data handling
import pandas as pd
# Bokeh libraries
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, Callback, CustomJS
from bokeh.models.widgets import DataTable, NumberFormatter, TableColumn, HTMLTemplateFormatter
from bokeh.io import curdoc
from bokeh.layouts import row, column
from bokeh.models.widgets import CheckboxGroup
# data
data = dict(group = ['A','A','A','A','A','A','A','B','B','B','B','B','B'],
length = [10, 20, 10, 20, 30, 20, 20, 30, 20, 30, 30, 20, 30],
weight = [100, 200, 100, 300, 100, 400, 100, 300, 100, 400, 500, 600, 450])
data = pd.DataFrame(data)
source = ColumnDataSource(data)
plot = figure()
plot.circle(x = 'length', y = 'weight', source = source)
color_checkbox = CheckboxGroup(labels=["all", "group"], active=[0])
color_checkbox.callback = CustomJS(args=dict(source = source), code='''
var source = source
var data = data;
var group = data['group'];
var selected_option = cb_obj.value;
var i, n = group.length;
for (i = 0; i < n; ++i) {
if (selected_option = 0);{
plot.circle.color = 'black'
}
else {
if (group[i] = 'A') {
plot.circle.color = 'green'
else
plot.circle.color = 'blue'
}
}
}
source.change.emit();
''')
widgets = column(color_checkbox)
layout = row(widgets, plot)
show(layout)