1

私は Python 2.7 (Anaconda) と matplotlib を使用して、「デスク」正方形パッチ -background- 上の正方形パッチのアニメーションを取得しているため、小さな正方形は必要な方向 (北、南、東、西) に沿って移動する必要があります。 range=n 位置 (move=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()

これは私の出力です

同じ図に n 回プロットする代わりに、問題を修正してパッチをアニメーション化する方法についての提案はありますか?

4

1 に答える 1