如何使用Python和amp;为多边形(由数组定义)制作动画Matplotlib

如何使用Python和amp;为多边形(由数组定义)制作动画Matplotlib

问题描述:

美好的一天!

问题说明:我想设置一个多边形的动画,该多边形的值是我从数组中收到的(在我的简单示例中,它是一个移动的正方形).我想让Polygon的x和y值保持可变.不用担心多边形会发生什么运动.这只是一个例子.像在'通过使用matplotlib '是通缉犯.

Problem explanation: I want to animate a Polygon which values I receive from an array (in my simple example it is a moving sqaure). I want to keep the Polygon's x-and y-values mutable. Dont worry about what movement the Polygon does. It is just an example. Working with "set_xy()" like in the solution from 'animation to translate polygon using matplotlib' is wanted.

目标->在每个动画帧中,我都希望从数组(P1x,P1y,P2x,P2y,...)中加载多边形值并更新图形.

Goal -> in every animation frame I want to load the Polygon values from the arrays (P1x,P1y,P2x,P2y,...) and update the figure.

问题:在我的代码中,使用补丁仍存在问题.我正在尝试使用索引i更新Polygon值.我必须如何定义补丁?是否必须通过动画调用来做到这一点?

Question: In my code I still have problems to work with the patches. I'm trying to update the Polygon values with the index i. How do I have to define the patch? Does this have to be done bevor the animation call?

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()方法来更新多边形的顶点.

Yes, you will need to create the Polygon first and add it to the axes. Inside the animating function you may use the patch's patch.set_xy() method to update the vertices of the 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]

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()