overlaying a plot repeatedly

I have a scatter plot that requires some time to render. The horizontal axis is time. Currently, I generate the full scatter plot each time and draw a axvline to indicate the progress of time, save the file as a png for each time, and generate a movie for all time frames. The scatter plot portion doesn’t change, just the vertical line.

I was wondering if there was a way to speed this up, to generate the scatter plot once and then overlay it with a moving axvline. I would still have to save the png frame by frame, but the most expensive step by far is replotting the scatter plot over and over.

Thanks!

This is what the animation/blit api is designed to do:

http://www.scipy.org/Cookbook/Matplotlib/Animations

scipy.org appears down right now, so you may need to try again later.
See also these mpl examples:

http://matplotlib.sourceforge.net/search.html?q=codex+blit

JDH

···

On Wed, Jul 29, 2009 at 1:01 AM, Art<grenander@...287...> wrote:

I have a scatter plot that requires some time to render. The horizontal axis
is time. Currently, I generate the full scatter plot each time and draw a
axvline to indicate the progress of time, save the file as a png for each
time, and generate a movie for all time frames. The scatter plot portion
doesn't change, just the vertical line.

I was wondering if there was a way to speed this up, to generate the scatter
plot once and then overlay it with a moving axvline. I would still have to
save the png frame by frame, but the most expensive step by far is
replotting the scatter plot over and over.

Thanks John. That made things more than an order of magnitude faster. It’s very impressive.

My bottleneck now is actually saving the pngs for mencoder to make into an avi (as with the movie_demo example on your site) using savefig. Do you also know of an alternative way of generating an avi of the animation? My animation has about 9000 frames.

Thanks,
Art.

···

On Wed, Jul 29, 2009 at 4:38 AM, John Hunter <jdh2358@…287…> wrote:

On Wed, Jul 29, 2009 at 1:01 AM, Art<grenander@…287…> wrote:

I have a scatter plot that requires some time to render. The horizontal axis

is time. Currently, I generate the full scatter plot each time and draw a

axvline to indicate the progress of time, save the file as a png for each

time, and generate a movie for all time frames. The scatter plot portion

doesn’t change, just the vertical line.

I was wondering if there was a way to speed this up, to generate the scatter

plot once and then overlay it with a moving axvline. I would still have to

save the png frame by frame, but the most expensive step by far is

replotting the scatter plot over and over.

This is what the animation/blit api is designed to do:

http://www.scipy.org/Cookbook/Matplotlib/Animations

scipy.org appears down right now, so you may need to try again later.

See also these mpl examples:

http://matplotlib.sourceforge.net/search.html?q=codex+blit

JDH

No, I can't imagine there is much fat to trim in that part of the
code. We're using libpng to write the png, so no speedups there
unless you can make the pngs smaller. It looks like we are using a
memory pointer to get the rgba data from agg over to png, so no
speedups there either. Perhaps Michael has some input. Some code
that we could run and test might help produce some further
optimizations.

JDH

···

On Wed, Jul 29, 2009 at 6:50 PM, Art<grenander@...287...> wrote:

My bottleneck now is actually saving the pngs for mencoder to make into an
avi (as with the movie_demo example on your site) using savefig. Do you also
know of an alternative way of generating an avi of the animation? My
animation has about 9000 frames.

John Hunter wrote:

My bottleneck now is actually saving the pngs for mencoder to make into an
avi (as with the movie_demo example on your site) using savefig. Do you also
know of an alternative way of generating an avi of the animation? My
animation has about 9000 frames.

No, I can't imagine there is much fat to trim in that part of the
code. We're using libpng to write the png, so no speedups there
unless you can make the pngs smaller. It looks like we are using a
memory pointer to get the rgba data from agg over to png, so no
speedups there either. Perhaps Michael has some input. Some code
that we could run and test might help produce some further
optimizations.

JDH

Can libpng be told to write one of the smaller png variants, using a color palette, for example, especially if antialiasing is turned off? For animation I suspect the reduced quality would not hurt much, and the smaller file size would be welcome, potentially yielding a smaller animation file as well.

Eric

···

On Wed, Jul 29, 2009 at 6:50 PM, Art<grenander@...287...> wrote:

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Below is some sample code that creates the directory ~/tmp/blit_test, outputs 50 pngs, and creates a mov.avi using /usr/local/bin/mencoder. Apologies in advance for the code:

import os
import numpy as np

import matplotlib.pyplot as plt
import subprocess

movdir = os.path.join(os.path.expanduser(‘~’), ‘tmp’, ‘blit_test’)
try: os.mkdirs(movdir)
except: pass
mencoder = ‘/usr/local/bin/mencoder’

ax =
im =
vl =

fig = plt.figure()

ax.append(fig.add_subplot(2,2,1))
im.append(ax[-1].imshow(np.random.randn(100,100)))
ax[-1].set_xticks()
ax[-1].set_yticks()

ax.append(fig.add_subplot(2,2,2))

im.append(ax[-1].imshow(np.random.randn(32,32)))
ax[-1].set_xticks()
ax[-1].set_yticks()

ax.append(fig.add_subplot(2, 1, 2))

r = np.abs(np.random.normal(0,0.1,1000))
for i in range(100):
ras = np.nonzero(np.random.poisson(r))[0]

ax[-1].scatter(ras, np.ones(len(ras)) * (i+1), s=1, alpha=0.5)

vl.append(ax[-1].axvline(-1, color=‘r’, alpha=0.9, linewidth=2.))
ax[-1].set_xlim([0,1000])
ax[-1].set_ylim([0,100])
plt.draw()

canvas = fig.canvas
background = canvas.copy_from_bbox(fig.bbox)

for i in range(50):
canvas.restore_region(background)
im[0].set_data(np.random.randn(100,100))
ax[0].draw_artist(im[0])

im[1].set_data(np.random.randn(32,32))
ax[1].draw_artist(im[1])

vl[0].set_xdata([i,i])
ax[2].draw_artist(vl[0])

canvas.blit()
plt.savefig(os.path.join(movdir, '%03d' % (i+1)))

fps = 10

command = (mencoder,
‘mf://%s/*.png’ % (movdir),
#‘-vf’, ‘scale=800:-10’,
‘-mf’, ‘fps=%d’ % fps,
‘-ovc’, ‘lavc’,

       '-lavcopts', 'vcodec=mpeg4',
       '-o', os.path.join(movdir, 'mov.avi'))

subprocess.check_call(command)

output in ~/tmp/blit_test/mov.avi

···

On Wed, Jul 29, 2009 at 6:09 PM, John Hunter <jdh2358@…985…> wrote:

On Wed, Jul 29, 2009 at 6:50 PM, Art<grenander@…287…> wrote:

My bottleneck now is actually saving the pngs for mencoder to make into an

avi (as with the movie_demo example on your site) using savefig. Do you also

know of an alternative way of generating an avi of the animation? My

animation has about 9000 frames.

No, I can’t imagine there is much fat to trim in that part of the

code. We’re using libpng to write the png, so no speedups there

unless you can make the pngs smaller. It looks like we are using a

memory pointer to get the rgba data from agg over to png, so no

speedups there either. Perhaps Michael has some input. Some code

that we could run and test might help produce some further

optimizations.

JDH