2016-10-09 6 views
1

私はPythonでアニメーションを持っています。私は更新するタイムラベルを追加したいと思います。私はすでにというNumPy配列を持っています。のように、変数をラベルに挿入するのと同じくらい簡単だと思いました。Pythonでアニメーションラベルを更新する

fig, ax = plt.subplots() 
line, = ax.plot([], lw=2) 
time_text = ax.text(0.02, 0.95, '', transform=ax.transAxes) 

plotlays, plotcols = [1], ["blue"] 
lines = [] 
for index in range(1): 
    lobj = ax.plot([], [], lw=2, color=plotcols[index])[0] 
    lines.append(lobj) 

def animate(i): 
    xlist = [xvals] 
    ylist = [psisol[i,:].real] 
    time_text.set_text('time = %0.2f' % time[i]) 

    for lnum, line in enumerate(lines): 
     line.set_data(xlist[lnum], ylist[lnum]) 

    return lines 

私はジェイクVanderplasの二重振り子のチュートリアルhereからそれを撮影した、とも私はこのStackOverflowのpostを見ました。プログラムの実行中は、プロット領域はグレーのままです。テキストコードをコメントアウトすると、プログラムは完璧に動作し、プロットやアニメーションが実行されます。他に何を試すかわからない

ありがとうございました。

答えて

1

グラフの中にアニメーションテキストを表示するために、作成したチュートリアルを編集しました。次回は、再現可能なコードを入力してください(xvalsは何ですか?psisol?+輸入品はありません)

""" 
Matplotlib Animation Example 

author: Jake Vanderplas 
email: [email protected] 
website: http://jakevdp.github.com 
license: BSD 
Please feel free to use and modify this, but keep the above information. Thanks! 
""" 

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

# First set up the figure, the axis, and the plot element we want to animate 
fig = plt.figure() 
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2)) 
line, = ax.plot([], [], lw=2) 
time_text = ax.text(0.02, 0.95, '', transform=ax.transAxes) 

# initialization function: plot the background of each frame 
def init(): 
    line.set_data([], []) 
    time_text.set_text('') 
    return line, 

# animation function. This is called sequentially 
def animate(i): 
    x = np.linspace(0, 2, 1000) 
    y = np.sin(2 * np.pi * (x - 0.01 * i)) 
    line.set_data(x, y) 
    time_text.set_text(str(i)) 
    return tuple([line]) + tuple([time_text]) 

# call the animator. blit=True means only re-draw the parts that have changed. 
anim = animation.FuncAnimation(fig, animate, init_func=init, 
           frames=200, interval=20, blit=True) 

plt.show()