Using colorbar in animation

Hi, I'm stacked again, sorry, but I couldn't find any pointers with my google.

In my filled contour animation I thought to use colorbar instead complicating with contour labeling, which just makes animation too full. So after reading the docs, I used something like this simplified code:

···

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

def init():
    return ax.cla(),

def animate(i):
    global t
    r = np.random.random(10)
    c = np.sin(2 * np.pi * r) * np.vstack(np.cos(2 * np.pi * r))
    ax.cla()
    cax = ax.contourf(c)
    cbar = fig.colorbar(cax, ticks=[0, .3, .6, 1])
    cbar.ax.set_yticklabels(['a', 'b', 'c', 'd'])
    return ax,

if __name__ == '__main__':
    fig = plt.figure()
    ax = fig.add_subplot(111)
    anim = animation.FuncAnimation(fig, animate, init_func=init,
                                   frames=100,
                                   interval=15,
                                   blit=True)
    anim.save('test.mp4', fps=15, extra_args=['-vcodec', 'libx264'])

and noticed that colorbar object is not cleared from the scene, but it repeatedly fills the plot until there are only colorbars on it. Here is sample frame:

http://i.imgur.com/ydoUFrO.png

And I have no idea how to clean it. Can anyone tell me how to clean this colorbar while iterating animation?

Thanks

I got a revelation - as colorbar wasn't changing with animation iteration I put it in init() function and draw the first frame there.
Voila :slight_smile: