Home > OS >  Can't animate quiver plot using the Player class for animation
Can't animate quiver plot using the Player class for animation

Time:11-08

I'm using the Player class for animation as defined here. It's pretty nice insofar as it includes a playback button console, and I've used it to accomplish a variety of settings, e.g. animating heatmaps.

However, I'm having problems animating vectors created using quiver.

I won't load all the code that defines the class itself, but here's my example, where I attempt to animate a sequence of arrows. The magnitude of the arrow at time t equals the value of the signal 2*sin(t):

fig,ax=plt.subplots(1)
t = np.linspace(0,6*np.pi, num=100)
s = 2*np.sin(t)
radius=s
x= np.multiply(radius,np.cos(t))
y =np.multiply( radius, np.sin(t))
point_vector,=ax.quiver(0,0,[],[])

def update(i):
    point_vector.set_UVC(x[i],y[i])

ani = Player(fig, update, maxi=len(t)-1)
plt.show()

The error message states

ValueError: Argument U has a size 0 which does not match 1, the number of arrow 
positions

which I believe arises from the quiver command itself.

CodePudding user response:

The following should do the trick. Note that:

  • I plotted the quiver giving it the initial direction. The error message was telling you exactly that: x, y and u, v must have the same size.
  • ax.quiver returns only a single handle, hence no comma before the equal sign.
  • I added a few keyword arguments to better visualize the quiver.
  • I adjusted axis limits for a better visualization.
fig,ax=plt.subplots(1)
t = np.linspace(0,6*np.pi, num=100)
s = 2*np.sin(t)
radius=s
x = np.multiply(radius,np.cos(t))
y = np.multiply( radius, np.sin(t))
point_vector=ax.quiver(0, 0, x[0], y[0], angles='xy', scale_units='xy', scale=1)
l = x.max()
if y.max() > l:
    l = y.max()
ax.set_xlim(-l, l)
ax.set_ylim(-l, l)
ax.set_aspect("equal")
  • Related