Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to configure FuncAnimation (dynamic plotting from matplotlib.animation) to numpy arrays?
    text
    copied!<p>I have some issues with the <em>FuncAnimation</em> fonction of <em>MatPlotLib</em>. I can't configure it to my code... I hope someone could help me !!</p> <p>This is a diffusion equation, I need to plot it for each step of the time. At each step, the result of the calculation is a numpy array. I manage to plot it in a dynamic way with <em>pyplot.interactive(True)</em> but it is very laggy. I read that <em>FuncAnimation</em> can deal with that problem but I did not manage to have it working with results in lists or arrays.</p> <p>Here is the code with a classic slow plot :<br> It produces a vector of vectors (<em>U</em>) wich are ploted after all calculations</p> <pre><code>import numpy as np import scipy from scipy.linalg import solve_banded from matplotlib import pyplot as plt import matplotlib.animation as animation def DrawRecord(U): plt.interactive(True) plt.figure(1) for i in range(0,len(U)): plt.clf() plt.plot(U[i]) plt.ylim([0,1]) plt.draw() J=350.0 dt=0.01 T=3.0 t=np.arange(dt,T,dt) dx=1.0/J D=0.005 c=0.5 r=0.1 mu=c*dt/(2.0*dx) lambd=D*dt/(dx**2.0) K_x=50.0*np.ones(J-1) alpha_t=0.5*np.ones(len(t)) #initial conditions u=np.zeros(J) u[J/5*1:J/5*2]=1 U=u espace=np.linspace(0,1,J) #Matrix A=np.diag(-lambd*np.ones(J-2),1)+np.diag((1+2*lambd)*np.ones(J-1),0)+np.diag(-lambd*np.ones(J-2),-1) AA=scipy.linalg.inv(A) for i in t: u[1:J]=scipy.dot(AA,u[1:J]+(r-alpha_t[i/dt])*dt*(u[1:J]-u[1:J]/K_x)) u[0]=0 u[J-1]=0 U=np.vstack([U,u]) DrawRecord(U) </code></pre> <p>And here is my try of making turn the <em>FuncAnimation</em> with the previous code (big fail) :<br> <em>nb :</em> U contents the arrays of results calculated for each steps </p> <pre><code>global U fig = plt.figure() window = fig.add_subplot(111) line, = window.plot(list(U[1,:])) def init(): line=list(U[1,:]) return line def animate(i): line.set_ydata(list(U[i,:])) return line anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=True) plt.show() </code></pre> <p>That produces a lot of errors ... Maybe someone can set it up for the previous code !<br> I hope I'm clear (sorry for my english) and thank you for your help.</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