saving animations

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()

I cannot see what is wrong, but after saving each figure you should
add plt.clf() in order to delete the image and preventing memory
leaks, because MPL stores one image in top of the other.

For the filecode I suggest you to use something like:

savefig(head+str(filecode).zfill(digits)+format, dpi=205)
filecode+=1

where head is the name, digits an int and format usually .png.

Which backend are you using? Do you need it to be displayed on screen?
If not, the backend Agg is best.

Regards,

David.

···

On Wed, Aug 25, 2010 at 5:28 AM, David Pine <djpine@...287...> 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()

------------------------------------------------------------------------------
Sell apps to millions through the Intel(R) Atom(Tm) Developer Program
Be part of this innovative community and reach millions of netbook users
worldwide. Take advantage of special opportunities to increase revenue and
speed time-to-market. Join now, and jumpstart your future.
http://p.sf.net/sfu/intel-atom-d2d
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

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

···

On Tue, Aug 24, 2010 at 10:28 PM, David Pine <djpine@…287…> 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()