Hi all,
I am relatively new to bokeh and want to start using it for my day to day data analysis as well as live monitoring at work. Basically what I am aiming for is a streaming line plot which has the added functionality of checkboxs to toggle on and off certain lines on my plot. Ive basically been combining the stock_app.py example as well as the random_walk.ipynb example. I’ve pasted what I have at the moment below. Im basically stuck at the point trying to get the checkboxs to toggle the lines on and off but Im not sure how this should be done. I would prefer to have this run through an ipython notebook if possible.
thanks for your help!
Mark
import time
import random
import numpy as np
import datetime
from bokeh.plotting import cursession, figure, show, output_server
from bokeh.models import GlyphRenderer, HoverTool, BoxSelectTool
from bokeh.models.widgets import Toggle
from bokeh.io import output_file, push, hplot,HBox
from bokeh.models.widgets import CheckboxGroup
from bokeh.models import (DatetimeAxis,
DatetimeTickFormatter, Grid, Legend, Plot, NumeralTickFormatter
)
class LinePlotStreamer(HBox):
def init(self):
self.checkbox_group = CheckboxGroup(labels=[“line 1”, “line 2”], active=[1])
self.setup_events()
x = np.array([datetime.datetime.now().time(),datetime.datetime.now().time()])#np.linspace(0,0)#datetime.datetime.now().time()#
y = np.array([0,0])
z = np.array([0,0])
#TOOLS = [BoxSelectTool(), HoverTool()]
toolset = “crosshair,pan,reset,resize,save,wheel_zoom”
self.p = figure(title=“Simple streaming example”, width=600,height=500,
tools=toolset, x_axis_type = “datetime”)
self.p.line(x,y, color="#2222aa", line_width=2, legend=‘line 1’)
self.p.line(x,z, color="#FB9A99", line_width=2, legend=‘line 2’)
xformatter = DatetimeTickFormatter(formats=dict(hours=["%H"],minutes=["%M"],seconds=["%S"]))
xaxis = DatetimeAxis(formatter=xformatter)
p.add_layout(xaxis, ‘below’)
self.p.yaxis[0].formatter = NumeralTickFormatter(format="$0.00")
#show(vform(text_input))
show(hplot(self.p,self.checkbox_group))
self.ds = self.p.select({“type”: GlyphRenderer})[0].data_source
self.ds2 = self.p.select({“type”: GlyphRenderer})[1].data_source
def setup_events(self):
self.checkbox_group.on_click(self.checkbox_handler)
def checkbox_handler(self,active):
print(“toggle switched”)
self.update_data(active)
def update_data(self,hide=False):
oldx = self.ds.data[“x”]
oldy = self.ds.data[“y”]
oldz = self.ds2.data[“y”]
rand = random.random()
rand2 = random.random()
newy = np.hstack([oldy, [rand]])
newz = np.hstack([oldz, [0.5]])
newx = np.hstack([oldx, datetime.datetime.now().time()])
plot_data_x =
plot_data_y =
plot_data_z =
for p in self.checkbox_group.active:
if p ==0:
plot_data_x = newx
plot_data_y = newy
if p ==1:
plot_data_x = newx
plot_data_z = newz
self.ds.data[“x”] = plot_data_x
self.ds.data[“y”] = plot_data_y
self.ds2.data[“x”] = plot_data_x
self.ds2.data[“y”] = plot_data_z
cursession().store_objects(self.ds)
cursession().store_objects(self.ds2)
output_server(“simple_stream”)
plot = LinePlotStreamer()
counter =0
while True:
counter += 1
plot.update_data()
time.sleep(1)