2017-11-04 5 views
0

私は425ファイル、Ex1、Ex2、Ex3、...、Ex425を持っています。各ファイルは1つのタイムステップに対応しています。各ファイルには、電界振幅の1つの列が含まれています。従って、時間t = 1において、Ex1は200点の空間内のフィールドであり、以下同様である。私は、これらのファイルの1次元アニメーションをPythonで作成したいが、それを達成できませんでした。誰も助けることができますか?1次元アニメーション

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

basename = "Ex" 
numFiles = 425 

fig = plt.figure() 
ax = plt.axes(xlim=(0, 200), ylim=(-2, 2)) 
line, = ax.plot([], [], lw=2) 

def init(): 
    line.set_data([], []) 
    return line, 

def animate(i): 
    x = np.linspace(0, 200, 1) 
    for t in range(1,numFiles): 
     filename = basename + str(t) 
     data = np.loadtxt(filename) 
    line.set_data(x, data[:,0]) 
    return line, 

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

plt.show() 
+0

matplotlibを使ったアニメーションはこれより少し複雑ですが、 'animation'モジュールが必要になります。 [ここでは、トピックに関する既存のチュートリアルはほとんどありません](https://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/)。 –

+0

多分、対話的なプロットはあなたにとって有益でしょう。 'plt.show()'の代わりに 'ion()'と 'plt.draw()'を使うこと。 [this](https://www.raspberrypi.org/learning/visualising-sorting-with-python/lesson-1/worksheet/) –

+0

私はこれらのチュートリアルを見ましたが、n0neはデータのインポートからどのようにアニメートするかを教えていません。さらに、データ= np.loadtxt(ファイル名)を使ってデータを読み込んでいるときには、pythonsは配列を作るので、プロットするのが難しくなります。 – Jitendra

答えて

0

[OK]を、最初のは、私が仮定形式でいくつかのファイルを生成してみましょうあなたでした:ここ

for i in range(11): 
    a = open("test%i.txt"%i, "w") 
    for j in np.random.rand(200)*i%5: 
     a.write(str(j)+"\n") 
    a.close() 

は動的にコンテンツをプロットする方法です:this answer

## this function reads your files 
# returns a list of strings 
def read_files(filename): 
    myfile = open(filename) 
    file_content= myfile.readlines() 
    myfile.close() 
    return file_content 

x = np.linspace(1, 200, 200) 
y = np.ones(200) * np.nan 

plt.ion() # enables figure updating 

fig = plt.figure() 
ax = fig.add_subplot(111) 

## configure the plot 
## these initial values for x and y will never be shown 
line1, = ax.plot(x, y, 'b') #  

## Loops through your files 
for File in glob.glob("test*.txt"): 
    y = read_files(File) 
    line1.set_ydata(y) 

    # recompute the ax.dataLim 
    ax.relim() 
    # update ax.viewLim using the new dataLim 
    ax.autoscale_view() 
    fig.canvas.draw() # display/update 

    time.sleep(2) 

ルックのための動的スケーリングに関する詳細。

関連する問題