2016-12-19 4 views
3

matplotlibのアニメーションモジュールでは、FFmpeg、mencoder、imagemagikなどのサードパーティ製モジュールを使用してアニメーションをファイル(例:https://stackoverflow.com/a/25143651/5082048)に保存する必要があります。matplotlib animation:サードパーティ製モジュールを使用しないpngファイルへの書き込み

matplotlibのMovieWriterクラスでさえ、第三者のモジュールが組み込まれるようになりました(プロセスの開始と終了、パイプ経由のcummunicating):http://matplotlib.org/api/animation_api.html#matplotlib.animation.MovieWriter

私は、matplotlib.animation.FuncAnimationオブジェクトフレームをpngのフレームに直接保存する方法を探しています。その後、私はこのアプローチを使用してiPythonノートにアニメーションとしての.pngファイルを表示したい:https://github.com/PBrockmann/ipython_animation_javascript_tool/

は、したがって、私の質問は以下のとおりです。

  • どのように私はせずに.pngファイルに直接matplotlib.animation.FuncAnimationオブジェクトを保存することができますサードパーティのモジュールを使用する必要がありますか?
  • このユースケースにはライタークラスが実装されていますか?
  • FuncAnimationオブジェクトからフレームごとにFigureオブジェクトを取得するにはどうすればよいですか?

編集:matplotlib.animation.FuncAnimationオブジェクトが与えられます。タスクは純粋なPythonを使用してフレームを保存することです。残念ながら、私は、ImportanceOfBeingErnestが提案するように、基礎となるアニメーション機能を変更することはできません。

答えて

1

あなたは

import matplotlib.animation as ma 


class BunchOFiles(ma.FileMovieWriter): 
    def setup(self, fig, dpi, frame_prefix): 
     super().setup(fig, dpi, frame_prefix, clear_temp=False) 

    def _run(self): 
     # Uses subprocess to call the program for assembling frames into a 
     # movie file. *args* returns the sequence of command line arguments 
     # from a few configuration options. 
     pass 

    def grab_frame(self, **savefig_kwargs): 
     ''' 
     Grab the image information from the figure and save as a movie frame. 
     All keyword arguments in savefig_kwargs are passed on to the 'savefig' 
     command that saves the figure. 
     ''' 

     # Tell the figure to save its data to the sink, using the 
     # frame format and dpi. 
     with self._frame_sink() as myframesink: 
      self.fig.savefig(myframesink, format=self.frame_format, 
          dpi=self.dpi, **savefig_kwargs) 

    def cleanup(self): 
     # explictily skip a step in the mro 
     ma.MovieWriter.cleanup(self) 

のようなもの(これはテストされていない、単にクラスを実装する方が良いかもしれません、あなたはおそらく、サブクラスFileMoveWriterしたい(http://matplotlib.org/2.0.0rc2/api/animation_api.html#writer-classesを参照)FileMovieWriterサブクラスを見てみたいですsavinggrab_framefinished、およびsetupを実装する

2

これは少し複雑に思えるかもしれませんが、アニメーションのフレームを保存することは、アニメーション自体で簡単に行うことができます。

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

def animate(i): 
    line.set_ydata(np.sin(2*np.pi*i/50)*np.sin(x)) 
    #fig.canvas.draw() not needed see comment by @tacaswell 
    plt.savefig(str(i)+".png") 
    return line, 

fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.set_xlim(0, 2*np.pi) 
ax.set_ylim(-1,1) 
x = np.linspace(0, 2*np.pi, 200) 
line, = ax.plot(x, np.zeros_like(x)) 
plt.draw() 

ani = matplotlib.animation.FuncAnimation(fig, animate, frames=5, repeat=False) 
plt.show() 

マインド継続的に実行するアニメーションを防ぎ、ディスクに同じファイルを書き込む繰り返しますrepeat = False引数。

+0

はい、あなたは絶対に正しいです。私がこのアプローチに向かない理由は、基本的にはアニメオブジェクトに直接アクセスするだけで、他のものはすべて既に設定されているからです。したがって、私は特にそのオブジェクトに直接アクセスする方法を探しています。 –

+0

私は参照してください。これは、質問に追加する重要な情報です。すべてのmoviewriterは一時的な画像をどこかに保存しなければならないので、原則的には簡単な作業でなければなりませんが、私は現在、他の解決策を認識していません。 – ImportanceOfBeingErnest

+0

もう一度あなたは正しいです。申し訳ありませんが、私は私の質問がはっきりしていないことに気付かなかった。 –

関連する問題