【第1話】プログラムの動作確認【Pythonコピペで量子力学完全攻略マニュアル】

#################################################################
## Pythonでグラフアニメーション
#################################################################
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
#図全体
fig = plt.figure(figsize=(10, 6))
#描画点数
NX = 100
#描画範囲
x_min = -1.0
x_max = 1.0
#座標点配列の生成
x = np.linspace(x_min, x_max, NX)
#アニメーション分割数
AN = 30
#アニメーション作成用
ims = []

#アニメーションの各グラフを生成
for a in range(AN):
    phi = 2.0 * np.pi * a / AN 
    #sin関数値を計算    
    y = np.sin( np.pi * x + phi )
    #各コマのグラフの描画
    img  = plt.plot(x, y, color="blue", linewidth=3.0, linestyle="solid" )
    ims.append( img )

#グラフタイトルの設定 
plt.title("sin function")
#x軸ラベル・y軸ラベルの設定 
plt.xlabel("x-axis")
plt.ylabel("y-axis")
#描画範囲を設定
plt.xlim([-1.0,1.0])
plt.ylim([-1.0,1.0])
#アニメーションの生成
ani = animation.ArtistAnimation(fig, ims, interval=50)
#アニメーションの保存
#ani.save("output.html", writer=animation.HTMLWriter())
#ani.save("output.gif", writer="imagemagick")
#ani.save("output.mp4", writer="ffmpeg", dpi=300)
#ani.save("output.mp4", writer=matplotlib.animation.AVConvWriter, dpi=300)
#グラフの表示
plt.show()

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です