2017-09-16 12 views
0

私はコーディングが新しく、以前に生成されたOctaveのコードからアニメーションを作成しようとしました。これまでのところ、私は無駄な散布図をプロットする以外に、何の成功もしていませんでした。すべてのヒントや助けをいただければ幸いです。OctaveコードからのPythonアニメーションの作成

clear; 

    N = 100; 
    NT = 200; 

    for i=1:N 
     x(i) = 0; 
     y(i) = 0; 
    end 

    plot(x,y,'o'); 
    axis([0 20 -10 10]); 

    for k = 1 : NT 
     x = x + normrnd(0.1,0.1,1,N); 
     y = y + normrnd(0,0.1,1,N); 
     temp = mod(k,1); 
     if temp == 0 

      plot(x,y,'s'); 
      axis([0 20 -10 10]); 
      drawnow 

     end 
    end 

ここでは動作しなかった多くの試み(以下)の1つです。

import numpy as np 
    import matplotlib as plt 
    from pylab import * 
    from matplotlib import animation 
    import random 

    n=100 
    nt=2000 
    m=20 
    mu=0 
    sigma=0.1 
    p=100 

    fig = plt.figure() 
    ax = plt.axes(xlim=(-10, 10), ylim=(-10, 10)) 
    scat, = ax.plot([], []) 

    # initialization function: plot the background of each frame 
    def init(): 
      x = 0 
      y = 0 
      return scat, 

    # animation function. This is called sequentially 
    def animate(i): 
     for k in range (1,nt): 
      x = x+ np.random.normal(mu, sigma, n) 
      return scat, 

     for i in range (1,nt): 
      y = y+ np.random.normal(mu, sigma, n) 
      return scat, 

    anim = animation.FuncAnimation(fig, animate, 
    init_func=init,frames=200, interval=20, blit=True) 

    plt.show() 

答えて

0

あなたは、Octave of Pythonを使ってアニメーションを行うことができます。

私は、問題の1つは、ループ内でリターンを設定することだと考えています。そのため、1つの関数defアニメーションで何回か繰り返します。アニメーションドキュメントhttps://matplotlib.org/api/animation_api.htmlを見て、私はあなたの例を編集し、私は便利かもしれないと思うどのこれを得た:

import numpy as np 
import matplotlib.pyplot as plt 
from pylab import * 
from matplotlib.animation import FuncAnimation 
import random 

n=100 
sigma=0.1 
nt=2000 

fig, ax = plt.subplots() 
xdata, ydata = [0.0], [0.0] 
ln, = plt.plot([], [], 'ro', animated=True) 

def init(): 
    ax.set_xlim( 0, 20) 
    ax.set_ylim(-10, 10) 
    return ln, 

def update(frame): 
    global xdata 
    global ydata 
    xdata = xdata + np.random.normal(0.1, sigma, n) 
    ydata = ydata + np.random.normal(0.0, sigma, n) 
    ln.set_data(xdata, ydata) 
    return ln, 

ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128), 
        init_func=init, blit=True) 
plt.show() 

あなたはまた、いくつかのPNGファイルを生成し、GIFまたはembbedを生成するためにGIMPを使用するオクターブの印刷コマンドを使用してアニメーションを行うことができますLaTeXではアニメーションを使用するpngを使用します。

ベスト、 ホルヘ

+0

おかげでホルヘ!パーティクルが指定された場所に到達すると停止する "フロア"を確立する方法はありますか? – lake08

関連する問題