From: Benjamin R. <ben...@ou...> - 2010-08-25 15:06:41
|
On Tue, Aug 24, 2010 at 10:28 PM, David Pine <dj...@gm...> wrote: > I am trying to save the frames from a matplotlib animation and I have a > question that is undoubtedly based on a profound lack of understanding about > how matplotlib/python works, but I'm hoping someone can offer me an > explanation. The following code is excerpted from the animation examples > "animation_blit_wx.py". The example works fine unless I include the #***** > line meant to record the animations in included. If the line is included, > the animated line is no longer drawn to the screen or to the sequence of > plots I am trying to save. That is, the background is displayed and saved, > but not the animated line. Why? > > > def update_line(*args): > global blit_time > > if update_line.background is None: > update_line.background = canvas.copy_from_bbox(ax.bbox) > > # restore the clean slate background > canvas.restore_region(update_line.background) > # update the data > line.set_ydata(npy.sin(x+update_line.cnt/10.0)) > # just draw the animated artist > ax.draw_artist(line) > # just redraw the axes rectangle > > t = time.time() > canvas.blit(ax.bbox) > blit_time += time.time() - t > #***** plt.savefig('f' + str(update_line.cnt) + '.png') ***** > > if update_line.cnt == NBLITS: > # print the timing info and quit > frame_time = time.time() - tstart > print '%d frames: %.2f seconds' % (NBLITS, frame_time) > print '%d blits: %.2f seconds' % (NBLITS, blit_time) > print > print 'FPS: %.2f' % (NBLITS/frame_time) > print 'BPS: %.2f' % (NBLITS/blit_time) > sys.exit() > > update_line.cnt += 1 > wx.WakeUpIdle() > > > Technically speaking, animation to the screen is completely different from what you are trying to do here. When showing an animation to the screen, a bunch of tricks are needed to make it efficient and for looping. However, if you only wish to save the individual frames, I would suggest that you just simply create your figures normally (none of this blitting and update_lines stuff) and save each of them as you would normally. Be sure to call clf() to prevent memory usage to grow out of control. Persoanlly, I then use ImageMagick to merge the image files together into an animated gif: convert `ls -1 *.png | sort -d` -set delay 40 -set dispose none -loop 0 animation.gif The experimental Animation module will have some preliminary support for saving animations (that feature isn't cross-platform right now). Ben Root |