Animation with an image in the backgroud

Good morning

I have a question regarding to animation in matplotlib using Blit.How
can I modify the example animation_blit_qt4.py that is in the
matplotlib website, in order to animate data, but instead of a white
background I want to have an image (tif or jpg)?

Many thanks for your help

German

I should first note that the way to do animations in matplotlib will probably be improved soon, the current methods should still be valid.

Ok, the way how I understand how blitting works is that a copy of the static background is made before any of the “sprites” are drawn. That static background is then redrawn at each frame to avoid having to do a complete re-render of it.

So, if you can display a particular image to a plot before calling copy_from_bbox(), but after the canvas.draw() call, then the animation should work as you would like with blitting, and you won’t need to repeat the function call to plot the background in the animation.

Note, I have not tried this, so please let us know how this works for you.
Ben Root

···

On Mon, Jul 5, 2010 at 5:45 AM, German Ocampo <gerocampo@…287…> wrote:

Good morning

I have a question regarding to animation in matplotlib using Blit.How

can I modify the example animation_blit_qt4.py that is in the

matplotlib website, in order to animate data, but instead of a white

background I want to have an image (tif or jpg)?

Many thanks for your help

German


This SF.net email is sponsored by Sprint

What will you do first with EVO, the first 4G phone?

Visit sprint.com/firsthttp://p.sf.net/sfu/sprint-com-first


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Benjamin

Thanks for your advice. I Modified the example in the way that you say
and now is working well. Basically I load the image before
copy_from_bbox and also I take out the self.ax.clear() in the
timerEvent. I'm including the program example with the animation of a
dot following a circle path with lena.png in the background.

Also I import this program in a Qt4 application and works well

Best regards

German

# For detailed comments on animation and the techniqes used here, see
# the wiki entry http://www.scipy.org/Cookbook/Matplotlib/Animations

import os
import sys

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

from PyQt4 import QtCore, QtGui

ITERS = 5

import Image
import time
import numpy as np

class BlitQT(FigureCanvas):

    def __init__(self):
        FigureCanvas.__init__(self, Figure())

        self.ax = self.figure.add_subplot(111)

        self.draw()

        file_image="lena.png"
        #The size of Lena png is 480x480
        self.im = Image.open(file_image)

        self.im1 = self.ax.imshow(self.im, origin='lower')

        self.old_size = self.ax.bbox.width, self.ax.bbox.height
        self.ax_background = self.copy_from_bbox(self.ax.bbox)

        self.cnt = 0
        self.acum_cnt=0
        self.angle = np.arange(0,2*np.pi,(2*np.pi)/100.)
        self.x=240+200*np.sin(self.angle)
        self.y=240+200*np.cos(self.angle)

        self.circle_line, = self.ax.plot(self.x[0],self.y[0],"o",
animated=True)
        self.draw()

        self.tstart = time.time()
        self.startTimer(10)

    def timerEvent(self, evt):
        current_size = self.ax.bbox.width, self.ax.bbox.height
        if self.old_size != current_size:
            self.old_size = current_size
            self.draw()
            self.ax_background = self.copy_from_bbox(self.ax.bbox)
        self.restore_region(self.ax_background)

        # update the data
        self.circle_line.set_xdata(self.x[self.cnt:self.cnt+1])
        self.circle_line.set_ydata(self.y[self.cnt:self.cnt+1])

        self.ax.draw_artist(self.circle_line)

        # just redraw the axes rectangle
        self.blit(self.ax.bbox)

        if self.cnt==99:
            self.acum_cnt+=1
            self.cnt=0

        else:
            self.cnt += 1

        if self.acum_cnt==ITERS:
            # print the timing info and quit
            print 'FPS:' , (ITERS*100)/(time.time()-self.tstart)
            sys.exit()

app = QtGui.QApplication(sys.argv)
widget = BlitQT()
widget.show()

sys.exit(app.exec_())

···

On Mon, Jul 5, 2010 at 6:09 PM, Benjamin Root <ben.root@...1304...> wrote:

I should first note that the way to do animations in matplotlib will
probably be improved soon, the current methods should still be valid.

Ok, the way how I understand how blitting works is that a copy of the static
background is made before any of the "sprites" are drawn. That static
background is then redrawn at each frame to avoid having to do a complete
re-render of it.

So, if you can display a particular image to a plot before calling
copy_from_bbox(), but after the canvas.draw() call, then the animation
should work as you would like with blitting, and you won't need to repeat
the function call to plot the background in the animation.

Note, I have not tried this, so please let us know how this works for you.
Ben Root

On Mon, Jul 5, 2010 at 5:45 AM, German Ocampo <gerocampo@...287...> wrote:

Good morning

I have a question regarding to animation in matplotlib using Blit.How
can I modify the example animation_blit_qt4.py that is in the
matplotlib website, in order to animate data, but instead of a white
background I want to have an image (tif or jpg)?

Many thanks for your help

German

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users