2016-10-11 3 views
0

プロットのデータを常に更新するリアルタイムプロットからムービーを作成したいと思います。 元のコードが長すぎて複雑なので、元のデータをより簡単なコードに置き換えて、ムービーの保存に関する問題に集中しています。リアルタイムプロット表示Python 3.6 - ムービーのリアルタイムプロットフィギュアウィンドウからムービーを保存しています

1)が正常に動作します

2)も正常に動作下にコードを表示リアルタイムプロットなしで映画を()保存

3)!!!しかし同時にリアルタイムでプロットして保存すると、ムービーのFigureウィンドウがゆっくりと上に移動するムービーが作成されます。

この問題の原因は何ですか?

映画のスクリーンショットのリンク: https://i.stack.imgur.com/1xWcM.jpg

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

# we generate some synthetic data. 
# we will make a list of X, Y lists. 
# the original code use for generating data is too long and complex to show here 
frames = [] 
frameNumber = 240 # we generate 240 different frames 

for i in range(0, frameNumber): 
    xData = np.linspace(0, 2 * np.pi) 
    yData = np.sin(xData - i * 2.0 * np.pi/frameNumber) 
    frames.append([xData, yData]) 
    print('frameNumber: ', i) 

# now we put them together to make a movie! let's set up the movie writer 
framerate = 60       # 24 frames per second 
# FFMpegWriter = manimation.writers['avconv'] 
FFMpegWriter = manimation.writers['ffmpeg'] 
metadata = dict(title='Wave Data', artist='Isaac Newton', comment='') 
writer = FFMpegWriter(fps=framerate, metadata=metadata) 

# figure setup 
fig,ax = plt.subplots() 
firstFrameX, firstFrameY = frames[0] 
l, = ax.plot(firstFrameX, firstFrameY, '-') 

plt.ylabel(r'$x$ axis') 
plt.xlabel(r'$y$ axis') 
plt.xlim(0, 2 * np.pi) 
plt.title(r'$\lambda = 2 \pi$') 

# --- without displaying ---> comment next 2 lines 
fig.show() 
fig.canvas.draw() 
# ------------------------------------------------ 

# let's write to the file! 
with writer.saving(fig, "anim.mp4", 100): 
    for i in range(frameNumber): 
     x, y = frames[i] 
     l.set_data(x, y) 

     # --- without displaying ---> comment next 4 lines 
     ax.draw_artist(ax.patch) 
     ax.draw_artist(l) 
     fig.canvas.update() 
     fig.canvas.flush_events() 
     # ------------------------------------------------ 
     writer.grab_frame() 

答えて

0

私は、Python 2.7でmatplotlib.animationを使用して同様の問題がありました。

初めてのコンソールでは、映画は問題ありませんが、スクリプトをもう一度実行すると、スクリーンショットのように結果として得られる映画が上に移動します。

コンソールを再起動すると問題が解決しましたが、あまり便利ではありません。

スクリプトの最後にplt.close(図)を追加して、問題を解決しました。

しかし、私はまだ最初にこの原因を正確には理解していません。

関連する問題