Bokeh Interactive Dashboard Can Not Remove Lines From Plot
I am working on my first python Bokeh interactive dashboard. Plot default shows lines for group=a and group=b. When check box[1], plot will add lines for group=a1 and group=b1. Whe
Solution 1:
Most likely, the problem (which you already corrected) was with the underscore in this line:
temp.append(selection1.labels[i]+ "_" + selection2.labels[b])
Which should be, of course:
temp.append(selection1.labels[i] + selection2.labels[b])
So you were referencing a_1
in the source (which doesn't exist) instead of a1
.
I felt free to improve your code to also hide the lines if you unselect the checkboxes. This code is for a Bokeh server v1.0.4 but should also work for Jupyter Notebook after removing the marked line block and uncomenting commented lines)
import random
import pandas as pd
from tornado.ioloop import IOLoop
from bokeh.server.server import Server
from bokeh.application import Application
from bokeh.application.handlers.function import FunctionHandler
from bokeh.plotting import figure, ColumnDataSource
from bokeh.models import CheckboxGroup, Panel, Tabs, WidgetBox, Row
from bokeh.palettes import Category10
data = [['a', 1, 0], ['a', 2, 1], ['a1', 1, 0], ['a1', 2, 2], ['b', 1, 0], ['b', 2, 3], ['b1', 1, 0], ['b1', 2, 4]]
df = pd.DataFrame(data, columns = ['group', 'time', 'rate'])
defmodify_doc(doc):
lines = []
defcreate_plots(to_plot):
for i inrange(len(to_plot)):
source = ColumnDataSource(
data = {'x':df.loc[df.group == to_plot[i]].time,
'group':df.loc[df.group == to_plot[i]].group,
'y':df.loc[df.group == to_plot[i]].rate})
lines.append(p3.line(x = 'x',
y = 'y',
source = source,
legend = to_plot[i],
color = (Category10[10])[i]))
p3.legend.click_policy = 'hide'defupdate(attr, old, new):
for i in [0, 1]:
if i notin selection1.active:
lines[i].visible = Falseelse:
lines[i].visible = Trueif selection2.active:
iflen(lines) < 3:
temp = []
for i in selection1.active:
lines[i].visible = Truefor b in selection2.active:
temp.append(selection1.labels[i] + selection2.labels[b])
create_plots(temp)
else:
for i inrange(2, 4):
if (i - 2) in selection1.active:
lines[i].visible = Trueelse:
lines[i].visible = Falseeliflen(lines) > 2:
for i inrange(2, 4):
if (i - 2) in selection1.active:
lines[i].visible = False
selection1 = CheckboxGroup(labels = ['a', 'b'], active = [0, 1], width = 40)
selection1.on_change('active', update)
selection2 = CheckboxGroup(labels = ['1'], width = 40)
selection2.on_change('active', update)
p3 = figure()
create_plots(['a', 'b'])
controls = WidgetBox(selection1, selection2, width = 40)
layout = Row(controls, p3)
tab = Panel(child = layout, title = 'test')
tabs = Tabs(tabs = [tab])
doc.add_root(tabs)
# handler = FunctionHandler(modify_doc)# app = Application(handler)#########################################################################
io_loop = IOLoop.current()
server = Server(applications = {'/myapp': Application(FunctionHandler(modify_doc))}, io_loop = io_loop, port = 5001)
server.start()
server.show('/myapp')
io_loop.start()
########################################################################## show(app)
Result:
Post a Comment for "Bokeh Interactive Dashboard Can Not Remove Lines From Plot"