python - Animating quiver in matplotlib -


i followed suggested code in response question, plotting animated quivers in python, i'm finding have problem 1 did not address. consider following code:

from matplotlib import pyplot plt import numpy np import matplotlib.animation animation  def ufield(x,y,t):     return np.cos(x+y)*np.sin(t)  def vfield(x,y,t):     return np.sin(x+y)*np.cos(t)  x = np.linspace(0,10, num=11) y = np.linspace(0,10, num=11) x,y = np.meshgrid(x,y) t = np.linspace(0,1)  def update_quiver(j, ax, fig):     u = ufield(x,y,t[j])     v = vfield(x,y,t[j])     q.set_uvc(u, v)     ax.set_title('$t$ = '+ str(t[j]))     return q,  fig =plt.figure() ax = fig.gca() u = ufield(x,y,t[0]) v = vfield(x,y,t[0]) q = ax.quiver(x, y, u, v) ax.set_title('$t$ = '+ str(t[0])) ax.set_xlabel('$x$') ax.set_ylabel('$y$')  ani = animation.funcanimation(fig, update_quiver,                                frames = range(0,t.size),                               interval=1,fargs=(ax, fig)) plt.show() 

this code runs fine, without problem. but, if want use init_func, , do, because want more animation, tried:

def init_quiver():     u = ufield(x,y,t[0])     v = vfield(x,y,t[0])     q = ax.quiver(x, y, u, v)     ax.set_title('$t$ = '+ str(t[0]))     ax.set_xlabel('$x$')     ax.set_ylabel('$y$')     return  q, 

remove these lines main body of script

u = ufield(x,y,t[0])  v = vfield(x,y,t[0])  q = ax.quiver(x, y, u, v) 

and update animation lines follows:

ani = animation.funcanimation(fig, update_quiver,                                frames = range(0,t.size),                               init_func=init_quiver,                               interval=1,fargs=(ax, fig)) plt.show() 

but when run this, see first frame, , nothing ever updates.

when run script, see behaviour describe, , in terminal run it, error traceback ending:

 file "quiv.py", line 21, in update_quiver q.set_uvc(u, v) nameerror: global name 'q' not defined 

you can rectify adding line

global q  

as first line of init_quiver function.

the complete code reads:

from matplotlib import pyplot plt import numpy np import matplotlib.animation animation  def ufield(x,y,t):     return np.cos(x+y)*np.sin(t)  def vfield(x,y,t):     return np.sin(x+y)*np.cos(t)  x = np.linspace(0,10, num=11) y = np.linspace(0,10, num=11) x,y = np.meshgrid(x,y) t = np.linspace(0,1)  def update_quiver(j, ax, fig):     u = ufield(x,y,t[j])     v = vfield(x,y,t[j])     q.set_uvc(u, v)     ax.set_title('$t$ = '+ str(t[j]))     return q,  def init_quiver():     global q     u = ufield(x,y,t[0])     v = vfield(x,y,t[0])     q = ax.quiver(x, y, u, v)     ax.set_title('$t$ = '+ str(t[0]))     ax.set_xlabel('$x$')     ax.set_ylabel('$y$')     return  q,  fig =plt.figure() ax = fig.gca() ax.set_title('$t$ = '+ str(t[0])) ax.set_xlabel('$x$') ax.set_ylabel('$y$')  ani = animation.funcanimation(fig, update_quiver,                                frames = range(0,t.size),                               init_func=init_quiver,                               interval=1,fargs=(ax, fig)) plt.show() 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -