Animating quiver in matplotlib
Question: I followed some suggested code in response to this question, [Plotting
animated quivers in
Python](http://stackoverflow.com/questions/19329039/plotting-animated-quivers-
in-python), but I'm finding I have a problem that one did not address.
consider the following code:

    
    
    from matplotlib import pyplot as plt
    import numpy as np
    import matplotlib.animation as 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 a problem. But, if I want to use an init_func,
and I do, because I want to do more with my animation, I 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 from the main body of the script

    
    
    u = ufield(X,Y,t[0]) 
    v = vfield(X,Y,t[0]) 
    Q = ax.quiver(X, Y, u, v)
    

and update the animation lines as 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 I run this, I see the first frame, and nothing ever updates.


Answer: When I run your script, I see the behaviour you describe, and in the terminal
where I run it, I get an error with a Traceback ending:

>
>      File "quiv.py", line 21, in update_quiver
>     Q.set_UVC(u, v)
>     NameError: global name 'Q' is not defined
>  

You can rectify this by adding the line

    
    
    global Q 
    

as the first line of your `init_quiver` function.

The complete code now reads:

    
    
    from matplotlib import pyplot as plt
    import numpy as np
    import matplotlib.animation as 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()
    

