【发布时间】:2018-10-30 18:33:52
【问题描述】:
美好的一天!
问题说明: 我想为从数组接收到的值制作一个多边形(在我的简单示例中,它是一个移动的正方形)。我想保持多边形的 x 和 y 值可变。不要担心 Polygon 做了什么运动。这只是一个例子。需要像在“animation to translate polygon using matplotlib”的解决方案中一样使用“set_xy()”。
目标 -> 在每个动画帧中,我想从数组 (P1x,P1y,P2x,P2y,...) 加载多边形值并更新图形。
问题: 在我的代码中,我仍然有使用补丁的问题。我正在尝试使用索引 i 更新 Polygon 值。我该如何定义补丁?这必须在动画调用之前完成吗?
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
import matplotlib.patches as patches
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim(-10,10)
ax.set_ylim(-10,10)
P1x=[0.0,0.5,1.0,1.5,2.0,2.5,3.0]
P1y=[0.0,0.0,0.0,0.0,0.0,0.0,0.0]
P2x=[1.0,1.5,2.0,2.5,3.0,3.5,4.0]
P2y=[0.0,0.0,0.0,0.0,0.0,0.0,0.0]
P3x=[1.0,1.5,2.0,2.5,3.0,3.5,4.0]
P3y=[1.0,1.0,1.0,1.0,1.0,1.0,1.0]
P4x=[0.0,0.5,1.0,1.5,2.0,2.5,3.0]
P4y=[1.0,1.0,1.0,1.0,1.0,1.0,1.0]
def init():
return patch,
def animate(i):
v = np.array([
[P1x[i], P1y[i]],
[P2x[i], P2y[i]],
[P3x[i], P3y[i]],
[P4x[i], P4y[i]]
])
patch=patches.Polygon(v,closed=True, fc='r', ec='r')
return patch,
ani = animation.FuncAnimation(fig, animate, np.arange(1, 5), init_func=init,
interval=1000, blit=True)
plt.show()
非常感谢您的帮助!
【问题讨论】:
-
@Bazingaa 我重新打开了,因为proposed duplicate 显示了如何更新集合补丁。但是这里只有一个补丁。所以一个集合对于这个简单的任务来说太复杂了。
标签: python animation matplotlib