Real-time graphs

John Hunter jdhunter at ace.bsd.uchicago.edu
Fri Oct 29 10:32:57 EDT 2004


>>>>> "MJR" == MJR  <pynode at centrum.cz> writes:

    MJR> Need to display data read from telemetry systme. Data might
    MJR> change in order of 1000/s. Not every sample will be
    MJR> displayed, but still need something close to 100sps --

Hmm, I assume sps means samples per second?  I was thinking about
refresh rates (frames per second) above, which is a different
question.  So the next question is, what kinds of refresh rates do you
need?

I wrote a little test script in matplotlib that randomly generates
1000 x,y points and displays them.  It can do about 12 frames per
second on my system (GTKAgg backend on a 3GHz P4 running linux).  With
only 100 points being plotted per frame, I get 40 FPS.  For more
complex plots, eg those which include images, or for other matplotlib
backends, this will be slower.

Here is a demo script - replace the random_data with your real data
and you're off to the races.  Note that matplotlib comes with a number
of examples for animations and dynamic images in the examples
subdirectory of the src distribution, or at
http://matplotlib.sf.net/examples

#!/usr/bin/env python
"""
Dynamically update line data
"""
import sys, time, os, gc
import matplotlib
matplotlib.use('GTKAgg')
from matplotlib import rcParams

from matplotlib.matlab import *
import gtk

fig = figure(1)
a = subplot(111)
def rand_data(N=100):
    x = arange(N)*0.001
    y = rand(N)
    return x,y

x,y = rand_data()
line, = plot(x,y,'-')


manager = get_current_fig_manager()
cnt = 0
tstart = time.time()
def updatefig(*args):
    global cnt, start
    x,y = rand_data()
    line.set_data(x,y)
    manager.canvas.draw()
    cnt += 1
    if cnt==50:
        print 'FPS', cnt/(time.time() - tstart)
        return gtk.FALSE
    return True


cnt = 0

gtk.idle_add(updatefig)
show()





JDH







More information about the Python-list mailing list