One word of caution: you'll notice that in the above,
> the xplt script runs very fast, while the mpl one is
> unacceptably slow (and it consumes a TON of cpu). There
> may be a trick to provide acceptable update speeds for
> dynamically resized plots, but unfortunately I don't use
> that kind of plotting much, so I can't really offer much
> help there.
Yes, this will run extremely slow, because each plot command creates a
new matplotlib.lines.Line2D object, which at draw time means a new
transformation, a new graphics context, etc. By the time you reach
the end of the loop, you'll have a ton of extra overhead in object
creation and function calls.
If possible, I suggest creating just two line objects and manipulating
their data directly, as in
from math import sin, cos
import matplotlib.numerix as nx
import pylab
pylab.ion() # interactive on, so each plot updates the window
pylab.hold('on')
allt = [0] # grow these lists and set the line data with them
allsin = [sin(0)]
allcos = [cos(0)]
lsin, = pylab.plot(allt, allsin, 'g+') # lsin is a Line2D instance
lcos, = pylab.plot(allt, allcos, 'ro') # lcos is a Line2D instance
pylab.xlabel('Time t[s]')
pylab.ylabel('Response')
pylab.axis([0,10,-1,1])
for t in nx.arange(0.1,10,0.1):
allt.append(t)
allsin.append(sin(t))
allcos.append(cos(t))
lsin.set_data(allt, allsin)
lcos.set_data(allt, allcos)
pylab.draw()
# to prevent the window from closing
raw_input()
> I get the feeling that there's an O(N^2) problem
> somewhere in there, because it seems to me that the plot
> update slows down worse than linearly as more points are
> added. But I didn't really measure it, it's just a gut
> feeling.
This will still be slower than xplt, but shouldn't be mind-numbingly
slow. I have some concrete ideas on how to make animation faster, and
have started working on them, but don't have anything ready yet.
JDH