Python real-time plotting

Here's how to draw graphs in real time using Bokeh.

First, install it:

pip install bokeh

Now add the following to a file named plot.py:

import random

from bokeh.driving import count
from bokeh.models import ColumnDataSource
from bokeh.plotting import curdoc, figure

UPDATE_INTERVAL = 100
ROLLOVER = 100

source = ColumnDataSource({"x": [], "y": []})

@count()
def update(x):
    y = random.random()
    source.stream({"x": [x], "y": [y]}, rollover=ROLLOVER)

p = figure()
p.line("x", "y", source=source)

doc = curdoc()
doc.add_root(p)
doc.add_periodic_callback(update, UPDATE_INTERVAL)

Then start plotting:

bokeh serve --show plot.py

Et voilĂ !

The code should be fairly straightforward and enough to get you started. UPDATE_INTERVAL is the interval between updates in milliseconds, and ROLLOVER is the number of displayed data points.

Bokeh can do a lot more, so be sure to check out their docs.

Home