Arrow animation in Python
<p>First of all, I am just starting to learn Python. I have been struggling during the last hours trying to update the arrow properties in order to change them during a plot animation.</p>

<p>After thoroughly looking for an answer, I have checked that it is possible to change a circle patch center by modifying the attribute 'center' such as <code>circle.center = new_coordinates</code>. However, I don't find the way to extrapolate this mechanism to an arrow patch...</p>

<p>The code so far is:</p>

<pre><code>import numpy as np, math, matplotlib.patches as patches
from matplotlib import pyplot as plt
from matplotlib import animation

# Create figure
fig = plt.figure()    
ax = fig.gca()

# Axes labels and title are established
ax = fig.gca()
ax.set_xlabel('x')
ax.set_ylabel('y')

ax.set_ylim(-2,2)
ax.set_xlim(-2,2)
plt.gca().set_aspect('equal', adjustable='box')

x = np.linspace(-1,1,20) 
y  = np.linspace(-1,1,20) 
dx = np.zeros(len(x))
dy = np.zeros(len(y))

for i in range(len(x)):
    dx[i] = math.sin(x[i])
    dy[i] = math.cos(y[i])
patch = patches.Arrow(x[0], y[0], dx[0], dy[0] )


def init():
    ax.add_patch(patch)
    return patch,

def animate(t):
    patch.update(x[t], y[t], dx[t], dy[t])   # ERROR
    return patch,

anim = animation.FuncAnimation(fig, animate, 
                               init_func=init, 
                               interval=20,
                               blit=False)

plt.show()
</code></pre>

<p>After trying several options, I thought that the function update could somehow take me closer to the solution. However, I get the error:</p>

<pre><code>TypeError: update() takes 2 positional arguments but 5 were given
</code></pre>

<p>If I just add one more patch per step by defining the animate function as shown below, I get the result shown in the image attached.</p>

<pre><code>def animate(t):
    patch = plt.Arrow(x[t], y[t], dx[t], dy[t] )
    ax.add_patch(patch)
    return patch,
</code></pre>

<p><a href="https://i.stack.imgur.com/fE2B1.png" rel="nofollow">Wrong animation</a></p>

<p>I have tried to add a patch.delete statement and create a new patch as update mechanism but that results in an empty animation...</p>

<p>I would be really grateful if anyone could bring some fresh air to this issue :)</p>

<p>Thanks in advance!</p>

<p>I found this by mimicking the code in <a href="https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/patches.py#L1125" rel="nofollow"><code>patches.Arrow.__init__</code></a>:</p>

<pre><code>import numpy as np
import matplotlib.patches as patches
from matplotlib import pyplot as plt
from matplotlib import animation
import matplotlib.transforms as mtransforms

# Create figure
fig, ax = plt.subplots()

# Axes labels and title are established
ax.set_xlabel('x')
ax.set_ylabel('y')

ax.set_ylim(-2,2)
ax.set_xlim(-2,2)
ax.set_aspect('equal', adjustable='box')

N = 20
x = np.linspace(-1,1,N) 
y  = np.linspace(-1,1,N) 
dx = np.sin(x)
dy = np.cos(y)

patch = patches.Arrow(x[0], y[0], dx[0], dy[0])

def init():
    ax.add_patch(patch)
    return patch,

def animate(t):
    L = np.hypot(dx[t], dy[t])

    if L != 0:
        cx = float(dx[t]) / L
        sx = float(dy[t]) / L
    else:
        # Account for division by zero
        cx, sx = 0, 1

    trans1 = mtransforms.Affine2D().scale(L, 1)
    trans2 = mtransforms.Affine2D.from_values(cx, sx, -sx, cx, 0.0, 0.0)
    trans3 = mtransforms.Affine2D().translate(x[t], y[t])
    trans = trans1 + trans2 + trans3
    patch._patch_transform = trans.frozen()
    return patch,

anim = animation.FuncAnimation(fig, animate, 
                               init_func=init, 
                               interval=20,
                               frames=N,
                               blit=False)

plt.show()
</code></pre>

python animation matplotlib arrow