blit example on MacOS with ipython

Hi,

I have taken the blit example and modified it such that it runs on my mac. The function is called blit_ex.py. It works well when I paste the code in ipython, but it doesn’t when I do “run blit_ex.py” from ipython.

I am using EPD Py25 4.1.30001_beta1 and Mac OS 10.5.

The error message is:

In [1]: run blit_ex.py

···

AssertionError Traceback (most recent call last)

/Users/pierre/Desktop/blit_ex.py in ()

24 canvas.restore_region(background)

25 line.set_ydata(np.sin(x+i/10.))

—> 26 ax.draw_artist(line)

27 canvas.blit(ax.bbox)

28

/Library/Frameworks/Python.framework/Versions/4.1.30001/lib/python2.5/site-packages/matplotlib-0.98.3.0001-py2.5-macosx-10.3-fat.egg/matplotlib/axes.pyc in draw_artist(self, a)

1532 data (axis ticks, labels, etc are not updated)

1533 “”"

-> 1534 assert self._cachedRenderer is not None

1535 a.draw(self._cachedRenderer)

1536

AssertionError:

WARNING: Failure executing file: <blit_ex.py>

And the code is

import matplotlib.pyplot as plt

import numpy as np

import time

plt.figure(1)

plt.clf()

ax = plt.subplot(111)

canvas = ax.figure.canvas

for profiling

tstart = time.time()

create the initial line

x = np.linspace(-3, 3, 1000)

line, = plt.plot(x, np.sin(x), animated=True)

save the clean slate background – everything but the animated line

is drawn and saved in the pixel buffer background

background = canvas.copy_from_bbox(ax.bbox)

for i in range(100):

canvas.restore_region(background)

line.set_ydata(np.sin(x+i/10.))

ax.draw_artist(line)

canvas.blit(ax.bbox)

Thanks!

Pierre

It looks to me that you need to force a figure draw before doing copy_from_bbox. Eg::

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

The reason you are seeing a difference between ipython “run” and ipython copy-and-paste is that in “run” mode ipython turns interactive drawing off (for details see http://matplotlib.sourceforge.net/users/shell.html) so the draw event is never called, and the cached renderer, which is triggering your exception, is never set.

This is speculation, as I haven’t tested, so please answer back if this fixes your problem and if so I will update the matplotlib animation recipe which also suffers from this problem (which is not exposed unless you are running in interactive mode, which you are). It is on my list of things to do to write a proper animation chapter for the user’s guide…

JDH

···

On Wed, Dec 10, 2008 at 5:20 PM, pierre garrigues <pierre.garrigues@…287…> wrote:

create the initial line

x = np.linspace(-3, 3, 1000)

line, = plt.plot(x, np.sin(x), animated=True)

save the clean slate background – everything but the animated line

is drawn and saved in the pixel buffer background

background = canvas.copy_from_bbox(ax.bbox)