2016-10-14 29 views
4

まず、Pythonを学び始めました。私はプロットアニメーション中に矢印プロパティを変更するために、矢印プロパティを更新しようとしている最後の時間に苦労しています。Pythonの矢印アニメーション

答えを徹底的に探した後、circle.center = new_coordinatesなどの属性 'center'を変更してサークルパッチセンターを変更できることを確認しました。しかし、私は矢印のパッチには、このメカニズムを推定する方法を見つけていない...

コード今のところです:

import numpy as np, math, matplotlib.patches as patches 
from matplotlib import pyplot as plt 
from matplotlib import animation 

# Create figure 
fig = plt.figure()  
ax = fig.gca() 

# Axes labels and title are established 
ax = fig.gca() 
ax.set_xlabel('x') 
ax.set_ylabel('y') 

ax.set_ylim(-2,2) 
ax.set_xlim(-2,2) 
plt.gca().set_aspect('equal', adjustable='box') 

x = np.linspace(-1,1,20) 
y = np.linspace(-1,1,20) 
dx = np.zeros(len(x)) 
dy = np.zeros(len(y)) 

for i in range(len(x)): 
    dx[i] = math.sin(x[i]) 
    dy[i] = math.cos(y[i]) 
patch = patches.Arrow(x[0], y[0], dx[0], dy[0]) 


def init(): 
    ax.add_patch(patch) 
    return patch, 

def animate(t): 
    patch.update(x[t], y[t], dx[t], dy[t]) # ERROR 
    return patch, 

anim = animation.FuncAnimation(fig, animate, 
           init_func=init, 
           interval=20, 
           blit=False) 

plt.show() 

いくつかのオプションを試した後、私は、機能更新が何とかできると思いました解決に近づけてください。しかし、私はエラーを取得する:

TypeError: update() takes 2 positional arguments but 5 were given 

私は、以下のようにアニメーション機能を定義することにより、ステップごとに1つの以上のパッチを追加した場合、私は、添付画像のような結果を得ることができます。

def animate(t): 
    patch = plt.Arrow(x[t], y[t], dx[t], dy[t]) 
    ax.add_patch(patch) 
    return patch, 

Wrong animation

私は模倣することでこれを見つけた...

+0

ヒント:あなたの初心者のような場合をあなたはそうだと言います、私は何かをこのようなものに動かす前に、もっと簡単に何かを試すことを提案します。 –

+0

あなたはおそらく正しいでしょう...私は、線と散点の2Dと3Dのアニメーションを正常に実装しました。アニメーション内の任意のオブジェクトを更新することを学ぶことは、可能性の世界を広げます:Pありがとう! –

+0

うれしい私はおいおいを助けることができました! –

答えて

2

ax.add_patch(patch)ax.clear()を追加しますが、あちこちのすべての要素を削除しますmプロット。

def animate(t): 

    ax.clear() 

    patch = plt.Arrow(x[t], y[t], dx[t], dy[t]) 
    ax.add_patch(patch) 

    return patch, 

EDIT:ax.patches.pop(index)を使用して1つのパッチ

  • を取り除きます。あなたはArrow

    def animate(t): 
    
        global patch 
    
        ax.patches.remove(patch) 
    
        patch = plt.Arrow(x[t], y[t], dx[t], dy[t]) 
        ax.add_patch(patch) 
    
        return patch, 
    
と外部 patch設定/取得することが globalを必要と ax.patches.remove(object)

を使用してindex=0

def animate(t): 

    ax.patches.pop(0) 

    patch = plt.Arrow(x[t], y[t], dx[t], dy[t]) 
    ax.add_patch(patch) 

    return patch, 
  • を使用することができますので、あなたの例では

    はわずか1つのパッチです


    ところで:あなたはあなたが色を変更するupdateを使用することができますupdate()

    print(patch.properties().keys()) 
    
    dict_keys(['aa', 'clip_path', 'patch_transform', 'edgecolor', 'path', 'verts', 'rasterized', 'linestyle', 'transform', 'picker', 'capstyle', 'children', 'antialiased', 'sketch_params', 'contains', 'snap', 'extents', 'figure', 'gid', 'zorder', 'transformed_clip_path_and_affine', 'clip_on', 'data_transform', 'alpha', 'hatch', 'axes', 'lw', 'path_effects', 'visible', 'label', 'ls', 'linewidth', 'agg_filter', 'ec', 'facecolor', 'fc', 'window_extent', 'animated', 'url', 'clip_box', 'joinstyle', 'fill']) 
    

    で使用できるプロパティのリストを取得する - `のFaceColor

    def animate(t): 
        global patch 
    
        t %= 20 # get only 0-19 to loop animation and get color t/20 as 0.0-1.0 
    
        ax.patches.remove(patch) 
    
        patch = patches.Arrow(x[t], y[t], dx[t], dy[t]) 
    
        patch.update({'facecolor': (t/20,t/20,t/20,1.0)}) 
    
        ax.add_patch(patch) 
    
        return patch, 
    
  • 1

    を私はpatch.delete文を追加して更新のメカニズムとして、新しいパッチを作成しようとしましたが、それは空のアニメーションになりpatches.Arrow.__init__のコード:

    import numpy as np 
    import matplotlib.patches as patches 
    from matplotlib import pyplot as plt 
    from matplotlib import animation 
    import matplotlib.transforms as mtransforms 
    
    # Create figure 
    fig, ax = plt.subplots() 
    
    # Axes labels and title are established 
    ax.set_xlabel('x') 
    ax.set_ylabel('y') 
    
    ax.set_ylim(-2,2) 
    ax.set_xlim(-2,2) 
    ax.set_aspect('equal', adjustable='box') 
    
    N = 20 
    x = np.linspace(-1,1,N) 
    y = np.linspace(-1,1,N) 
    dx = np.sin(x) 
    dy = np.cos(y) 
    
    patch = patches.Arrow(x[0], y[0], dx[0], dy[0]) 
    
    def init(): 
        ax.add_patch(patch) 
        return patch, 
    
    def animate(t): 
        L = np.hypot(dx[t], dy[t]) 
    
        if L != 0: 
         cx = float(dx[t])/L 
         sx = float(dy[t])/L 
        else: 
         # Account for division by zero 
         cx, sx = 0, 1 
    
        trans1 = mtransforms.Affine2D().scale(L, 1) 
        trans2 = mtransforms.Affine2D.from_values(cx, sx, -sx, cx, 0.0, 0.0) 
        trans3 = mtransforms.Affine2D().translate(x[t], y[t]) 
        trans = trans1 + trans2 + trans3 
        patch._patch_transform = trans.frozen() 
        return patch, 
    
    anim = animation.FuncAnimation(fig, animate, 
               init_func=init, 
               interval=20, 
               frames=N, 
               blit=False) 
    
    plt.show()