Parts of plot lagging from previous frame in animation

I am unable to get matplotlib animations to work properly. Some of the data from
the previous frame is getting included.

I have included some example code that shows this. When draw() is called in the
loop, it draws the data from the previous frame for y<0.15 (ca), and from the
current frame for larger y. The final show() does *not* lag even if set_ydata
has not been called since the last draw().

This is with version 0.72. Any ideas about what is happening?

-- code follows

# animate increasing terms in fourier expansion of y=x
from pylab import *
from time import sleep

samples = 1000
max_k = 3

x = arange(0.0, 1.0, 1.0/samples)

s = zeros((max_k,samples), Float)

for k in range(1,max_k):
    s[k] = s[k-1]+(-1)**(k+1)*sin(pi*x*k)/k

ion() # to force window creation
line, = plot(x, x*1.3, linewidth=1.0)
grid(True)
ioff()

for k in range(1,max_k):
    line.set_ydata(2/pi*s[k])
    title('k = '+str(k))
    draw()
    sleep(1)

show()

···

--
j