On Tue, Dec 20, 2011 at 8:00 AM, Nils Wagner <nwagner@iam.uni-stuttgart.de> wrote:
Hi all,

How do I use animation.FuncAnimation to plot real-life
data from parsing a text file ?


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

import sys
import time
import re

x   = []         # x
y   = []         # y

fig   = plt.figure()
ax    = fig.add_subplot(111)
curve,= ax.plot([],[],lw=2)
ax.set_xlim(0,5)
ax.grid()

def tail_f(file):
  interval = 1.0

  while True:
    where = file.tell()      # current file position, an
integer (may be a long integer).
    line = file.readline()
    if re.search('without errors',line): break
    if not line:
      time.sleep(interval)
      file.seek(where)       # seek(offset[, whence]) ->
None.  Move to new file position.
    else:
      yield line


def run():
    for line in tail_f(open(sys.argv[1])):
        print line,
        if re.search('x=',line):
            liste = line.split('=')
            x.append(liste[1].strip())
        if re.search('y=',line):
            liste = line.split('=')
            y.append(liste[1].strip())

            curve.set_data(x,y)
            print x,y
#
#
#
run()
plt.show()


The text file looks like

x=0.0
y=0.0
blabla
x=1.0
y=1.0
blabla
x=2.0
y=4.0
blabla
...



Nils


Nils,

I think the key thing to keep in mind when using any of the animators is that the animator in question is driving the calls to update the plot from its own event source.  In most cases, that source is a timer.  For FuncAnimator, the function passed into the constructor must perform whatever actions are needed for a single update.

What I would do is Subclass FuncAnimator so that its constructor will create an empty Line2D object that has already been added to an axes object (or you can pass an empty one yourself as an argument to the function).  In the function run(), you would obtain the next chunk of data and then update the Line2D object with that information.

I think you have it mostly done, just need a few extra pieces.

Cheers!
Ben Root