I ran the script with GTKCairo and it looks fine to me.
> Perhaps you are using GTKAgg which looks like it has the
> problem. My guess is that its caused by using
> gtk.idle_add() which results in asynchronous expose_event
> updates.
I looked at this briefly and am a little confused by something. The
base class FigureCanvasGTK.expose_event has this in the doc string
"""Expose_event for all GTK backends
Should not be overridden.
but then the derived class
class FigureCanvasGTKAgg(FigureCanvasGTK, FigureCanvasAgg):
does in fact override it.
I inserted a debug print statement into
FigureCanvasGTKAgg.expose_event and it is not called until the loop is
over. I think this is because the call to sleep is preventing the
idle handler from ever getting engaged.
Perhaps Joachim would be better off using a gtk timer to handle his
animation. Basically, matplotlib tries to provide a GUI neutral way
to do animation (eg anim.py) but if you want to do something
semi-sophisticated like alter the timing between draws, you should use
the GUI specific functionality for this, eg gtk timers.
Here is an example
# animate increasing terms in fourier expansion of y=x
import pygtk
pygtk.require('2.0')
import gtk
import matplotlib
matplotlib.use('GTKAgg')
from matplotlib.numerix import arange, pi, sin, zeros, Float
import pylab
class Animator:
def __init__(self, samples=1000, max_k=30):
self.samples = samples
self.max_k = max_k
self.fig = pylab.figure()
self.ax = self.fig.add_subplot(111)
x = arange(0.0, 1.0, 1.0/self.samples)
self.s = zeros((self.max_k,samples), Float)
for k in range(1,self.max_k):
self.s[k] = self.s[k-1]+(-1)**(k+1)*sin(pi*x*k)/k
self.line, = self.ax.plot(x, x*1.3, linewidth=1.0)
self.ax.grid(True)
self.k = 1
def draw(self, *args):
self.line.set_ydata(2./pi*self.s[self.k])
self.ax.set_title('k = '+str(self.k))
pylab.draw()
self.k += 1
return self.k<self.max_k
anim = Animator()
gtk.timeout_add(100,anim.draw)
pylab.show()