【发布时间】:2016-10-14 02:25:45
【问题描述】:
首先,我刚刚开始学习 Python。在过去的几个小时里,我一直在努力更新箭头属性以便在绘图动画期间更改它们。
在彻底寻找答案后,我检查是否可以通过修改属性“中心”来更改圆形补丁中心,例如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 语句并创建一个新补丁作为更新机制,但这会导致一个空动画...
【问题讨论】:
-
提示:如果您像您所说的那样是初学者,我建议您先尝试非常更简单的方法,然后再进行此类操作。
-
你可能是对的......我已经成功实现了线条和散点的 2D 和 3D 动画,我只想更进一步。学习更新动画中的任何随机对象打开了一个充满可能性的世界:P 感谢您的提示!
-
很高兴我能帮助老兄!
标签: python animation matplotlib