This Page

old_animation example code: strip_chart_demo.pyΒΆ

[source code]

Emulate an oscilloscope.  Requires the animation API introduced in
matplotlib 0.84.  See for an

This example uses gtk but does not depend on it intimately.  It just
uses the idle handler to trigger events.  You can plug this into a
different GUI that supports animation (GTKAgg, TkAgg, WXAgg) and use
your toolkits idle/timer functions.
import gobject
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D

class Scope:
    def __init__(self, ax, maxt=10, dt=0.01): = ax
        self.canvas = ax.figure.canvas
        self.dt = dt
        self.maxt = maxt
        self.tdata = [0]
        self.ydata = [0]
        self.line = Line2D(self.tdata, self.ydata, animated=True)
        self.background = None
        self.canvas.mpl_connect('draw_event', self.update_background), 1.1), self.maxt)

    def update_background(self, event):
        self.background = self.canvas.copy_from_bbox(

    def emitter(self, p=0.01):
        'return a random value with probability p, else 0'
        v = np.random.rand(1)
        if v>p: return 0.
        else: return np.random.rand(1)

    def update(self, *args):
        if self.background is None: return True
        y = self.emitter()
        lastt = self.tdata[-1]
        if lastt>self.tdata[0]+self.maxt: # reset the arrays
            self.tdata = [self.tdata[-1]]
            self.ydata = [self.ydata[-1]]
  [0], self.tdata[0]+self.maxt)


        t = self.tdata[-1] + self.dt
        self.line.set_data(self.tdata, self.ydata)

        return True

fig, ax = plt.subplots()
scope = Scope(ax)

Keywords: python, matplotlib, pylab, example, codex (see Search examples)