2016-10-25 1 views
1

Python Matplotlibアニメーションに奇妙な問題があります。私はコーナーにもう一つの小さなビデオを含むビデオを作りたいと思っています。大きなプロットはいくつかのデータをアニメートする必要があり、挿入された小さなプロットはグローバルデータプロットの時間位置を動いている破線で示すべきです。 plot.show()だけを使用すると、すべてうまく動作します。ちょうど私が望む方法です。しかし、私がplot.show()にコメントし、ani.saveで保存しようとすると、保存されるのは大きなプロットであり、挿入されたものは消えます!Matplotlibアニメーションは、ffmpegでビデオの一部分のみを保存します。

同時にani.saveとplot.show()のコメントを解除しようとすると、「AttributeError:draw_artistはレンダリングをキャッシュする初期描画の後にしか使用できません」というエラーメッセージが表示され、アニメーション画面は空白のままです。

解決方法を教えてください。私は完全な映画で保存されたファイルが必要です。ここで

コードです:

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

Q_data = np.arange(100.).reshape((10,10)) 
age = np.arange(10.) 
Q_total = np.arange(10.) 

Q = Q_data[1:,:] 
r_ax = np.arange(10.) 


fig, ax = plt.subplots() 
line, = ax.semilogy(r_ax,Q[0,:]) 
time_template = 'time = %.1f' 
time_text = ax.text(0.58, 0.9, '', size=20, transform=ax.transAxes) 
plt.title('Plot 1') 
ax.set_xlabel('Axis Title') 
ax.set_ylabel('Axis Title') 
# this is another inset axes over the main axes  
a = plt.axes([0.2, 0.5, .3, .3]) 
line2, = plt.semilogx(age,Q_total,color='r',linewidth=1)  
plt.title('Plot 2', y=1.11) 
a.set_xlabel('Small axis title') 
#fig.canvas.draw() 

def animate(i): 
    line.set_ydata(Q[i,:]) # update the data 
    time_text.set_text(time_template % (age[i])) 
    a.lines.pop() 
    a.semilogx(age,Q_total,color='r',linewidth=1) 
    a.axvline(age[i], color='k', linestyle='--', linewidth=1)   


    return line, a, time_text 

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


ani = animation.FuncAnimation(fig, animate, np.arange(1, Q.shape[0], 1),  init_func=init, interval=25, blit=True) 

ani.save('testanimation.avi', writer="ffmpeg", fps=15)         

plt.show() 

答えて

0

私はあなたに大きな軸とそれの中に小さな軸と小さな例を作りました。

import numpy as np 
import matplotlib.pyplot as plt 
plt.rcParams['animation.ffmpeg_path'] = r'D:\ffmpeg-20161029-1e660fe-win64-static\bin\ffmpeg.exe' 
import matplotlib.animation as animation 

r_ax = np.arange(1, 10., 0.1) 

fig, ax = plt.subplots() 
line, = ax.plot(r_ax, np.log(r_ax)) 
a = plt.axes([0.2, 0.5, .3, .3]) 
line2, = a.plot([], [],color='r',linewidth=1) 
a.set_xlim([1, 10]) 
a.set_ylim([0, 3]) 

def animate(i): 
    line.set_data(r_ax[:i], np.log(r_ax[:i])) # update the data 
    line2.set_data(r_ax[:i], 3-np.log(r_ax[:i])) 

    return line, line2 

ani = animation.FuncAnimation(fig, animate, frames=100, interval=35, blit=True) 
ani.save('testanimation.avi', writer="ffmpeg", fps=15) 

# plt.show() 
+0

ありがとうございました!しかし、このコードは、私と同様の問題がありますが、はるかに優れています。保存されたファイルを開くと、フレームだけが表示されますが、動く線は表示されません。彼らは何らかの理由で救われません。 Plt.show()は全てを正しく表示しますが、.aviファイルは表示しません。 – Kristina

+0

私のコードは私のために働き、.aviファイルは動いている行を表示します。私はPython 2.7.11、matplotlib 1.5.1、ffmpeg 3.1.5を使用します。私もpython 3.5.1と同じものを試してみました。 –

+0

私の編集した答えを見てください(私はちょうど最後の行をコメントアウトしました) –

関連する問題