0

I am using a for loop to calculate values at every node of 20x20 matrix and storing data in

MM = [] 

I want to animate the results and my code looks like this:

ax = plt.subplot(111) 
for i in range(60): 
    x = MM[i]  
    ax.contourf(X,Y,x, cmap = cm.hot) 
    plt.draw()                            

plt.show()

The problem is that it shows only MM[-1]. I have looked over the examples given here, but can't figure out how to make it work. Thank you.

cvut
  • 53
  • 1
  • 7
  • related: http://stackoverflow.com/q/16915966/1461210, http://stackoverflow.com/q/23070305/1461210 – ali_m Apr 27 '15 at 18:19

1 Answers1

0

Your problem is likely due how you are running Matplotlib and what graphical backend you are using. The following example works in IPython. Note that I call ion() to set the interactive mode to on.

from matplotlib import pyplot as plt
import numpy as np

x = y = np.arange(-3.0, 3.01, 0.025)
X, Y = np.meshgrid(x, y)

plt.ion()
ax = plt.subplot(111) 
for i in range(1,20): 
    Z1 = plt.mlab.bivariate_normal(X, Y, 0.5+i*0.1, 0.5, 1, 1)
    ax.contourf(x,y,Z1, cmap = plt.cm.hot) 
    plt.draw()    

plt.show()

The information here should help you get your animation running.

Molly
  • 13,240
  • 4
  • 44
  • 45