Note that there are some explanatory texts on larger screens.

plurals
  1. POAnimation with contours matplotlib
    text
    copied!<p>I am trying to animate contours in matplotlib 1.1.0</p> <p>Based on: <a href="http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg17614.html">http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg17614.html</a>, <a href="http://matplotlib.1069221.n5.nabble.com/Matplotlib-1-1-0-animation-vs-contour-plots-td18703.html">http://matplotlib.1069221.n5.nabble.com/Matplotlib-1-1-0-animation-vs-contour-plots-td18703.html</a> and <a href="http://www.scipy.org/Cookbook/Matplotlib/Gridding_irregularly_spaced_data">http://www.scipy.org/Cookbook/Matplotlib/Gridding_irregularly_spaced_data</a></p> <pre><code>from numpy import linspace,exp,vstack from scipy.interpolate import griddata import matplotlib.pyplot as plt import matplotlib.animation as animation from numpy.random import uniform, seed def main(): seed(1234) x = uniform(-2,2,100) y = uniform(-2,2,100) data = vstack((x*exp(-x**2-y**2),0.5*x*exp(-x**2-y**2),0.2*x*exp(-x**2-y**2))) xi = linspace(min(x), max(x)) yi = linspace(min(y), max(y)) zi = [] numframes = data.shape[0] for ii in range(numframes): zi.append(griddata((x, y), data[ii], (xi[None,:], yi[:,None]), method='cubic')) fig = plt.figure() im = plt.contour(xi, yi, zi[0], 15, linewidths=0.5, colors='k') ani = animation.FuncAnimation(fig, update_contour_plot, frames=xrange(numframes), fargs=(zi, im, fig, xi, yi), interval=100) plt.colorbar() plt.show() def update_contour_plot(i, data, im, fig, xi, yi): for coll in im.collections: try: plt.gca().collections.remove(coll) except ValueError: #Everything is not removed for some reason! pass im = plt.contour(xi, yi, data[i], 15, linewidths=0.5, colors='k') plt.title(str(i)) return im, main() </code></pre> <p>1) Is this the best way to do this? Mention otherwise.</p> <p>2) In the final output, contour lines from previous frame are still visible. How do I remove them? See except ValueError:</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload