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

import random

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

ROLLOVER = 100 # Number of displayed data points

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

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

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

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

Then start plotting:

bokeh serve --show

It will launch a browser and display the real-time graph.