如何使用Python设置多边形(由数组定义)的动画&;Matplotlib
你好 问题解释: 我想设置一个多边形的动画,该多边形的值是我从一个数组接收的(在我的简单示例中,它是一个移动的sqaure)。我想保持多边形的x和y值可变。不要担心多边形的运动。这只是一个例子。需要在来自“”的解决方案中使用类似的“set_xy()” 目标->在每个动画帧中,我要从数组(P1x,P1y,P2x,P2y,…)加载多边形值并更新图形 问题: 在我的代码中,我仍然存在使用补丁的问题。我正在尝试用索引I更新多边形值。如何定义修补程序?这必须在动画调用之前完成吗如何使用Python设置多边形(由数组定义)的动画&;Matplotlib,python,animation,matplotlib,Python,Animation,Matplotlib,你好 问题解释: 我想设置一个多边形的动画,该多边形的值是我从一个数组接收的(在我的简单示例中,它是一个移动的sqaure)。我想保持多边形的x和y值可变。不要担心多边形的运动。这只是一个例子。需要在来自“”的解决方案中使用类似的“set_xy()” 目标->在每个动画帧中,我要从数组(P1x,P1y,P2x,P2y,…)加载多边形值并更新图形 问题: 在我的代码中,我仍然存在使用补丁的问题。我正在尝试用索引I更新多边形值。如何定义修补程序?这必须在动画调用之前完成吗 import matplo
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()
非常感谢你的帮助 是的,您需要首先创建多边形并将其添加到轴。在动画功能中,您可以使用面片的
patch.set_xy()
方法来更新多边形的顶点
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]
P = np.concatenate((np.array([P1x, P2x, P3x, P4x]).reshape(4,1,len(P1x)),
np.array([P1y, P2y, P3y, P4y]).reshape(4,1,len(P1x))), axis=1)
patch = patches.Polygon(P[:,:,0],closed=True, fc='r', ec='r')
ax.add_patch(patch)
def init():
return patch,
def animate(i):
patch.set_xy(P[:,:,i])
return patch,
ani = animation.FuncAnimation(fig, animate, np.arange(P.shape[2]), init_func=init,
interval=1000, blit=True)
plt.show()
是的,您需要首先创建多边形并将其添加到轴。在动画功能中,您可以使用面片的
patch.set_xy()
方法来更新多边形的顶点
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]
P = np.concatenate((np.array([P1x, P2x, P3x, P4x]).reshape(4,1,len(P1x)),
np.array([P1y, P2y, P3y, P4y]).reshape(4,1,len(P1x))), axis=1)
patch = patches.Polygon(P[:,:,0],closed=True, fc='r', ec='r')
ax.add_patch(patch)
def init():
return patch,
def animate(i):
patch.set_xy(P[:,:,i])
return patch,
ani = animation.FuncAnimation(fig, animate, np.arange(P.shape[2]), init_func=init,
interval=1000, blit=True)
plt.show()
@Bazingaai重新打开,因为显示了如何更新修补程序集合。但这里只有一个补丁。所以对于这个简单的任务来说,集合太复杂了。@Bazingaa我重新打开了,因为显示了如何更新补丁集合。但这里只有一个补丁。因此,对于这个简单的任务来说,集合太复杂了。