I'm looking for a way to update my contour lines in an animation that doesn't require me to replot the figure each time.
Most responses to this question I've found advocate recalling ax.contour
, but as my contours are superimposed on another image this is unbearably slow.
The only response I've found that looks close to answering the question answers it with a dead link: Animating a contour plot in matplotlib using FuncAnimation
EDIT: this is probably the intended link.
Example code:
#!/usr/bin/env python
import matplotlib.pylab as plt
import matplotlib.animation as anim
from matplotlib.colors import LinearSegmentedColormap as lsc
import numpy
#fig = 0; ax = 0; im = 0; co = 0
image_data = numpy.random.random((100,50,50))
contour_data = numpy.random.random((100,50,50))
def init():
global fig, ax, im, co
fig = plt.figure()
ax = plt.axes()
im = ax.imshow(image_data[0,:,:])
co = ax.contour(contour_data[0,:,:])
def func(n):
im.set_data(image_data[n,:,:])
co.set_array(contour_data[n,:,:])
init()
ani = anim.FuncAnimation(fig, func, frames=100)
plt.show()
Cheers.