John Titus Jungao John Titus Jungao -4 years ago 143
Python Question

Streaming two line graphs using bokeh

I would like to create a visualization where there are two line graphs which are updated with one new point per line graph per second.
The result will be something like this.

I have recently read about bokeh and found out that it can be used in visualizing streams of data in real time. However don't know how to code in it yet.

I would appreciate it if someone can show me how this task can be done using bokeh. thanks!

Answer Source

For bokeh-0.11.1:

Basically, you need to run you python app in the bokeh server. Then anyone can connect to the server and view the graph in realtime.

First, write your program. Use this code for example:

from bokeh.plotting import figure, curdoc
from bokeh.driving import linear
import random

p = figure(plot_width=400, plot_height=400)
r1 = p.line([], [], color="firebrick", line_width=2)
r2 = p.line([], [], color="navy", line_width=2)

ds1 = r1.data_source
ds2 = r2.data_source

def update(step):['x'].append(step)['y'].append(random.randint(0,100))['x'].append(step)['y'].append(random.randint(0,100))  


# Add a periodic callback to be run every 500 milliseconds
curdoc().add_periodic_callback(update, 500)

Then run the server from the command line, with your program:

C:\>bokeh serve --show

This will open the browser with your realtime graph.

For all the details see the bokeh server documentation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download