python - 如何使用 Python 和 matplotlib 获取动画补丁而不是 n 次绘制的补丁

标签 python animation matplotlib patch figures

我正在使用Python 2.7(Anaconda)和matplotlib来获取“ table ”正方形补丁-背景-上的正方形补丁的动画,以便小正方形必须沿着所需的方向移动(北,南,东) ,西)对于范围=n个位置(移动=1)。我第一次使用 FuncAnimation,但作为绘图,我只绘制了 n 次正方形,而不是让它移动/动画。这是我的代码,示例方向为“南”:

import matplotlib.pyplot as plt
import matplotlib.patches as ptc
from matplotlib import animation
import numpy as np
#__________________________________________________________________________PLOT
fig = plt.figure()
ax1 = fig.add_subplot(111,aspect='equal')

ax1.set_xlim((0,10))
ax1.set_ylim((0,10))

#________________________________________________FIGURES_VECTORS_INITIALIZATION
#DESK 
desk=np.matrix([[1,1],[9,1],[9,9],[1,9]]) #desk vertices
#setting initialization - DESK
def initDesk():
    ax1.add_patch(ptc.Polygon(desk, closed=True,
                          fill=False, hatch="/"))    

#SQUARE
squVer=np.matrix([[4.5,6.5],[5.5,6.5],[5.5,7.5],[4.5,7.5]]) #square vertices

#_____________________________________________________DIRECTIONS_INITIALIZATION 
move=1
null=0
#4DIRECTIONS
north=np.array(([+null,+move]))
south=np.array([+null,-move])
est=np.array([+move,+null])
west=np.array([-move,+null])

#_____________________________________________________________________ANIMATION
def animate(newPos):
    iniSqu=np.matrix(squVer)
    position=newPos

    for step in range (0,4):
        if step < 1: #starting point
            newPos=iniSqu
        else: 
            newPos=iniSqu+position
        square=ptc.Polygon(newPos, closed=True,
                             facecolor="blue")
        ax1.add_patch(square)
        iniSqu=newPos

anim = animation.FuncAnimation(fig, animate(south),init_func=initDesk(),repeat=True)

plt.show()

This is my output

关于如何解决问题并使补丁动画化而不是在同一个图形上绘制 n 次的建议?

最佳答案

您误解了 FuncAnimation 的工作方式。它的签名是

FuncAnimation(figure, func, frames, ...)

其中 func 是重复调用的函数,frames 是数字、列表或数组,或者生成器。

根据frames,每个时间步都会使用新参数调用函数func。在上面的代码中,该函数已经为每个调用执行了所有操作,这当然是不希望的。相反,它应该为每个调用做不同的事情。

此外,您不应自己调用该函数,而应仅将其提供给 FuncAnimaton 类,然后该类将调用它。 所以它实际上是 FuncAnimation(figure, func,frames, ...) 而不是 FuncAnimation(figure, func(something),frames, ...)

要制作动画,使正方形向南移动四次,frames 将是一个类似 frames = [south,south,south,south] 的列表。

import matplotlib.pyplot as plt
import matplotlib.patches as ptc
from matplotlib import animation
import numpy as np

fig = plt.figure()
ax1 = fig.add_subplot(111,aspect='equal')

ax1.set_xlim((0,10))
ax1.set_ylim((0,10))

desk=np.matrix([[1,1],[9,1],[9,9],[1,9]]) #desk vertices
#SQUARE
squVer=np.matrix([[4.5,6.5],[5.5,6.5],[5.5,7.5],[4.5,7.5]]) #square vertices
iniSqu=[squVer]

move=1
null=0
#4DIRECTIONS
north=np.array(([+null,+move]))
south=np.array([+null,-move])
est=np.array([+move,+null])
west=np.array([-move,+null])

deskpoly = [ptc.Polygon(desk, closed=True, fill=False, hatch="/")]
squarepoly = [ptc.Polygon(iniSqu[0], closed=True, facecolor="blue")]

def initDesk():
    ax1.clear()
    ax1.set_xlim((0,10))
    ax1.set_ylim((0,10))
    iniSqu[0] = squVer
    ax1.add_patch(deskpoly[0])
    ax1.add_patch(squarepoly[0])

def animate(direction):
    iniSqu[0] = iniSqu[0] + direction
    squarepoly[0].remove()
    squarepoly[0] = ptc.Polygon(iniSqu[0], closed=True, facecolor="blue")
    ax1.add_patch(squarepoly[0])

frames = (south, south, south, south)
anim = animation.FuncAnimation(fig, animate, frames=frames, init_func=initDesk,repeat=True)

plt.show()

enter image description here

关于python - 如何使用 Python 和 matplotlib 获取动画补丁而不是 n 次绘制的补丁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44620263/

相关文章:

python - Matplotlib - 如何用海岸线和国家叠加等高线图

python - VS代码: How to debug Flask app that uses Connexion?

python - Canonical Celery 单文件 hello world

swift - 导航栏下方的菜单在 Swift 中带有动画

css - 为什么动画迭代不适用于延迟?

html - 在纯 css 图片上向下滑动

python - 如何在Python中更改绘图的边界框线宽?

Python-通过ftp获取文件的行数

python - Python无法打开JSON文件,给出JSONDecodeError

python - matplotlib 直方图中的圆条