Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when saving Matplotlib Animation

I've tried to save a Matplotlib animation, but am getting an odd error. The snippet of (I believe) relevant code is:

  def showMovie(self):
    frameFnc = self.getNoisyImage
    initFnc =  self.initImage

    movie = animation.FuncAnimation(self.movieFig, frameFnc,
                                frames = len(self.noiseArrays),
                                init_func = initFnc,
                                interval=1, blit=True,
                                repeat = True)


    movie.save("test.mp4", fps=10, extra_args=['-vcodec', 'libx264'])
    print "Saved movie"
    plt.ion()
    plt.show()



if __name__ == '__main__':
    z = noiseTester()
    z.makeStaticNoiseImages()
    z.readPredList()
    z.showMovie()

The error I get is as follows:

Traceback (most recent call last):
  File "noiseTest6.py", line 134, in <module>
    z.showMovie()
  File "noiseTest6.py", line 123, in showMovie
    movie.save("test.mp4", fps=10, extra_args=['-vcodec', 'libx264'])
  File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 718, in save
    writer.grab_frame(**savefig_kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 204, in grab_frame
    dpi=self.dpi, **savefig_kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1421, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 2220, in print_figure
**kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 497, in print_raw
    renderer._renderer.write_rgba(filename_or_obj)
  RuntimeError: Error writing to file

When I check, filename_or_obj is an ', mode 'wb' at 0x7f3de99164b0>. This variable also has a name attribute, with a value of ''. The meaning of this string value, instead of the name I tried to assign to the file is not clear to me.

I know my animation is OK, because I've seen it work. It only has 500 frames, so I don't think it's too large. If anyone has any ideas about how I should chase down this Runtime error, I'd really appreciate it.

like image 890
user1245262 Avatar asked Sep 23 '26 22:09

user1245262


1 Answers

Well, using the info about libx264 from @hitzg , I just got rid of the arguments specifying its use - i.e.

movie.save("test.mp4", fps=10, extra_args=['-vcodec', 'libx264'])

was replaced by

movie.save("test.mp4", fps=10)

Now, things seem to be working.

like image 154
user1245262 Avatar answered Sep 25 '26 11:09

user1245262