Python中的箭头动画
首先,我刚刚开始学习Python。在过去的几个小时里,我一直在努力更新箭头属性,以便在绘图动画中更改它们 在彻底查找答案后,我检查了是否可以通过修改属性“center”(例如Python中的箭头动画,python,animation,matplotlib,Python,Animation,Matplotlib,首先,我刚刚开始学习Python。在过去的几个小时里,我一直在努力更新箭头属性,以便在绘图动画中更改它们 在彻底查找答案后,我检查了是否可以通过修改属性“center”(例如circle.center=new_坐标)来更改圆面片中心。然而,我没有找到将这种机制外推到箭头补丁的方法 到目前为止,守则是: import numpy as np, math, matplotlib.patches as patches from matplotlib import pyplot as plt from
circle.center=new_坐标)来更改圆面片中心。然而,我没有找到将这种机制外推到箭头补丁的方法
到目前为止,守则是:
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
如果我只是通过定义如下所示的动画功能,在每一步添加一个补丁,我会得到所附图像中显示的结果
def animate(t):
patch = plt.Arrow(x[t], y[t], dx[t], dy[t] )
ax.add_patch(patch)
return patch,
我试图添加一个patch.delete语句并创建一个新的补丁作为更新机制,但结果是一个空动画…我通过模仿以下代码发现了这一点:
在ax.Add\u patch(patch)
之前添加ax.clear()
,但将从绘图中删除所有元素
def animate(t):
ax.clear()
patch = plt.Arrow(x[t], y[t], dx[t], dy[t] )
ax.add_patch(patch)
return patch,
编辑:删除一个修补程序
- 使用
ax.patches.pop(索引)
在您的示例中,只有一个补丁,因此您可以使用index=0
def animate(t):
ax.patches.pop(0)
patch = plt.Arrow(x[t], y[t], dx[t], dy[t] )
ax.add_patch(patch)
return patch,
- 使用
ax.patches.remove(对象)
它需要global
来获取/设置带有箭头的外部补丁
def animate(t):
global patch
ax.patches.remove(patch)
patch = plt.Arrow(x[t], y[t], dx[t], dy[t] )
ax.add_patch(patch)
return patch,
顺便说一句:获取可与update()一起使用的属性列表。
因此,您可以使用update
更改颜色-facecolor
def animate(t):
global patch
t %= 20 # get only 0-19 to loop animation and get color t/20 as 0.0-1.0
ax.patches.remove(patch)
patch = patches.Arrow(x[t], y[t], dx[t], dy[t])
patch.update({'facecolor': (t/20,t/20,t/20,1.0)})
ax.add_patch(patch)
return patch,
小贴士:如果你是一个像你说的那样的初学者,我建议你先尝试一些更简单的方法,然后再将一种方法转移到像这样的方法上。你可能是对的。。。我已经成功地实现了线和散乱点的二维和三维动画,我只想更进一步。学习更新动画中的任何随机对象将打开一个充满可能性的世界:P谢谢你的提示!很高兴我能帮上忙,伙计!
print( patch.properties().keys() )
dict_keys(['aa', 'clip_path', 'patch_transform', 'edgecolor', 'path', 'verts', 'rasterized', 'linestyle', 'transform', 'picker', 'capstyle', 'children', 'antialiased', 'sketch_params', 'contains', 'snap', 'extents', 'figure', 'gid', 'zorder', 'transformed_clip_path_and_affine', 'clip_on', 'data_transform', 'alpha', 'hatch', 'axes', 'lw', 'path_effects', 'visible', 'label', 'ls', 'linewidth', 'agg_filter', 'ec', 'facecolor', 'fc', 'window_extent', 'animated', 'url', 'clip_box', 'joinstyle', 'fill'])
def animate(t):
global patch
t %= 20 # get only 0-19 to loop animation and get color t/20 as 0.0-1.0
ax.patches.remove(patch)
patch = patches.Arrow(x[t], y[t], dx[t], dy[t])
patch.update({'facecolor': (t/20,t/20,t/20,1.0)})
ax.add_patch(patch)
return patch,