Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Streaming two line graphs using bokeh

I want to create a visualization where there are two line graphs that are updated with one new point per line graph per second.

I have recently read about bokeh and found out that it can be used in visualizing streams of data in real-time. However, I 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!

like image 597
jtitusj Avatar asked Jun 09 '16 11:06

jtitusj


People also ask

How do you plot multiple lines on bokeh?

Bokeh can be used to plot multiple lines on a graph. Plotting multiple lines on a graph can be done using the multi_line() method of the plotting module.

Is bokeh better than Plotly?

In this comparison of Bokeh vs Plotly, we can't make out a decisive choice between the two. Though Plotly is good for plotting graphs and visualizing data for insights, it is not good for making dashboards. To make dashboards we can use bokeh and can have very fast dashboards and interactivity.


2 Answers

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:

# myplot.py
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

@linear()
def update(step):
    ds1.data['x'].append(step)
    ds1.data['y'].append(random.randint(0,100))
    ds2.data['x'].append(step)
    ds2.data['y'].append(random.randint(0,100))  
    ds1.trigger('data', ds1.data, ds1.data)
    ds2.trigger('data', ds2.data, ds2.data)

curdoc().add_root(p)

# 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 myplot.py

This will open the browser with your realtime graph.

For all the details see the bokeh server documentation.

like image 112
Elad Joseph Avatar answered Oct 14 '22 07:10

Elad Joseph


You can add scrolling to your graph by adding the following to the plot figure declaration:

p = figure(plot_width=400, plot_height=400)
p.x_range.follow="end"
p.x_range.follow_interval = 20
p.x_range.range_padding=0

where the follow_interval = the number of points that accumulate on the on the graph before it starts scrolling. I believe you can set the visible range on the chart, as well. FYI I got the scrolling code from the OHLC example on the bokeh GitHub page found here: https://github.com/bokeh/bokeh/tree/master/examples/app The OHLC is an example of streaming data using the "...= new_data" technique that bigreddot mentioned.

like image 21
Alan Tate Avatar answered Oct 14 '22 06:10

Alan Tate