pythonmatplotlibmplot3dmatplotlib-animationmatplotlib-3d

Animating a 3D vector


I need to animate both the magnitude and the position of a vector arrow in a 3D figure with matplotlib. As far as I can tell, I cannot animate the position of the vectors with Axes3D.quiver and there is no matplotlib.pyplot.arrow for a 3D figure. Is there a way to do this or do I need to look at other options?


Solution

  • quiver does not have an updating function, but you can easily remove an old quiver and plot a new one in each frame.

    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib.animation import FuncAnimation
    
    fig, ax = plt.subplots(subplot_kw=dict(projection="3d"))
    
    def get_arrow(theta):
        x = np.cos(theta)
        y = np.sin(theta)
        z = 0
        u = np.sin(2*theta)
        v = np.sin(3*theta)
        w = np.cos(3*theta)
        return x,y,z,u,v,w
    
    quiver = ax.quiver(*get_arrow(0))
    
    ax.set_xlim(-2, 2)
    ax.set_ylim(-2, 2)
    ax.set_zlim(-2, 2)
    
    def update(theta):
        global quiver
        quiver.remove()
        quiver = ax.quiver(*get_arrow(theta))
    
    ani = FuncAnimation(fig, update, frames=np.linspace(0,2*np.pi,200), interval=50)
    plt.show()
    

    quiver animation