2

初期角度で初期速度で発射された発射体のパスをアニメーション化しようとしています。ここにあるコードを変更しようとしました: http://matplotlib.org/examples/animation/simple_anim.html

私のコードは次のようになります。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

g = 9.8                               #value of gravity
v = 20                                #initial velocity
theta = 20*np.pi/180                  #initial angle of launch in radians
tt = 2*v*np.sin(theta)/g              #total time of flight
t = np.linspace(0, tt, 0.01)          #time of flight into an array
x = v*np.cos(theta)*t                 #x position as function of time
line, = ax.plot(x, v*np.sin(theta)*t-(0.5)*g*t**2) #plot of x and y in time

def animate(i):
    line.set_xdata(v*np.cos(theta)*(t+i/10.0))
    line.set_ydata(v*np.sin(theta)*(t+i/10.0)-(0.5)*g*(t+i/10.0)**2)  
    return line,

#Init only required for blitting to give a clean slate.
def init():
    line.set_xdata(np.ma.array(t, mask=True))
    line.set_ydata(np.ma.array(t, mask=True))
    return line,

ani = animation.FuncAnimation(fig, animate, np.arange(1, 200),
init_func=init, interval=25, blit=True)
plt.show()

示されているように、コードはプロット ウィンドウを表示しますが、軌道もアニメーションも表示しません。これが他の場所で尋ねられたかどうかを確認するためにここで検索しましたが、まだ見つかりません。質問されている場合は、既に回答された質問にリンクしてください。どんな助けでも大歓迎です。皆さんありがとう。

4

1 に答える 1

4

Python 3.4.3で次の作業を行うことができました:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

g = 9.8                                                        #value of gravity
v = 10.0                                                       #initial velocity
theta = 40.0 * np.pi / 180.0                                   #initial angle of launch in radians
t = 2 * v * np.sin(theta) / g                                  
t = np.arange(0, 0.1, 0.01)                                    #time of flight into an array
x = np.arange(0, 0.1, 0.01)
line, = ax.plot(x, v * np.sin(theta) * x - (0.5) * g * x**2)   # plot of x and y in time

def animate(i):
    """change the divisor of i to get a faster (but less precise) animation """
    line.set_xdata(v * np.cos(theta) * (t + i /100.0))
    line.set_ydata(v * np.sin(theta) * (x + i /100.0) - (0.5) * g * (x + i / 100.0)**2)  
    return line,

plt.axis([0.0, 10.0, 0.0, 5.0])
ax.set_autoscale_on(False)

ani = animation.FuncAnimation(fig, animate, np.arange(1, 200))
plt.show()

軸を再スケーリングする必要があり、他の多くのことが変更されました。

完璧ではありませんが、必要な軌道をたどる発射体を示しています。

今すぐ遊んで、コードを見て、いじって学習することができます。また、numpy/pyplot の基本を学ぶために少し時間を費やすことをお勧めします。それは将来的に多額の配当を支払うでしょう。

于 2015-09-26T15:55:37.677 に答える