首先,我刚开始学习Python。在过去的几个小时里,我一直在努力尝试更新箭头属性,以便在绘制动画期间更改它们。
在彻底寻找答案之后,我发现可以通过修改“center”属性(例如 circle.center = new_coordinates
)来更改圆形补丁中心。然而,我不知道如何将此机制推广到箭头补丁中...
目前的代码为:
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()
尝试了几个选项后,我认为更新功能可以在某种程度上让我更接近解决方案。然而,我遇到了一个错误:
TypeError: update() takes 2 positional arguments but 5 were given
如果我按照下面所示的定义animate函数的方式,每一步只添加一个补丁,则会得到附图所示的结果。
def animate(t):
patch = plt.Arrow(x[t], y[t], dx[t], dy[t] )
ax.add_patch(patch)
return patch,
我尝试添加了一个patch.delete语句并创建了一个新的patch作为更新机制,但结果是一个空动画...