This Page

old_animation example code: animation_blit_gtk2.pyΒΆ

[source code]

#!/usr/bin/env python

from __future__ import print_function

This example utlizes restore_region with optional bbox and xy
arguments.  The plot is continuously shifted to the left. Instead of
drawing everything again, the plot is saved (copy_from_bbox) and
restored with offset by the amount of the shift. And only newly
exposed area is drawn. This technique may reduce drawing time for some cases.

import time

import gtk, gobject

import matplotlib

import numpy as np
import matplotlib.pyplot as plt

class UpdateLine(object):
    def get_bg_bbox(self):


    def __init__(self, canvas, ax):
        self.cnt = 0
        self.canvas = canvas = ax

        self.prev_time = time.time()
        self.start_time = self.prev_time
        self.prev_pixel_offset = 0.

        self.x0 = 0
        self.phases = np.random.random_sample((20,)) * np.pi * 2
        self.line, = ax.plot([], [], "-", animated=True, lw=2)

        self.point, = ax.plot([], [], "ro", animated=True, lw=2), 1.1)

        self.background1 = None

        cmap =
        from itertools import cycle
        self.color_cycle = cycle(cmap(np.arange(cmap.N)))

    def save_bg(self):
        self.background1 = self.canvas.copy_from_bbox(

        self.background2 = self.canvas.copy_from_bbox(self.get_bg_bbox())

    def get_dx_data(self, dx_pixel):
        tp =
        x0, y0 = tp((0, 0))
        x1, y1 = tp((dx_pixel, 0))
        return (x1-x0)

    def restore_background_shifted(self, dx_pixel):
        restore bacground shifted by dx in data coordinate. This only
        works if the data coordinate system is linear.

        # restore the clean slate background

        # restore subregion (x1+dx, y1, x2, y2) of the second bg
        # in a offset position (x1-dx, y1)
        x1, y1, x2, y2 = self.background2.get_extents()
                                   bbox=(x1+dx_pixel, y1, x2, y2),
                                   xy=(x1-dx_pixel, y1))

        return dx_pixel

    def on_draw(self, *args):
        return False

    def update_line(self, *args):

        if self.background1 is None:
            return True

        cur_time = time.time()
        pixel_offset = int((cur_time - self.start_time)*100.)
        dx_pixel = pixel_offset - self.prev_pixel_offset
        self.prev_pixel_offset = pixel_offset
        dx_data = self.get_dx_data(dx_pixel) #cur_time - self.prev_time)

        x0 = self.x0
        self.x0 += dx_data
        self.prev_time = cur_time, self.x0+0.1)

        # restore background which will plot lines from previous plots
        self.restore_background_shifted(dx_pixel) #x0, self.x0)
        # This restores lines between [x0-2, x0]


        # now plot line segment within [x0, x0+dx_data],
        # Note that we're only plotting a line between [x0, x0+dx_data].
        xx = np.array([x0, self.x0])

        # the for loop below could be improved by using collection.
         for p in self.phases]

        self.background2 = canvas.copy_from_bbox(self.get_bg_bbox())


         for p in self.phases]


        dt = (time.time()-tstart)
        if dt>15:
            # print the timing info and quit
            print('FPS:' , self.cnt/dt)
            raise SystemExit

        self.cnt += 1
        return True

plt.rcParams["text.usetex"] = False

fig, ax = plt.subplots()
canvas = fig.canvas

fig.subplots_adjust(left=0.2, bottom=0.2)

# for profiling
tstart = time.time()

ul = UpdateLine(canvas, ax)

canvas.mpl_connect('draw_event', ul.on_draw)

Keywords: python, matplotlib, pylab, example, codex (see Search examples)