Non-blocking, from IPython, debugging

Suppose you want to test matplotlib interactively from IPython without preparing a figure by hand. In this case you have to import a piece of code (mpl.py) by:

In [1]: from mpl import *

Here is the contents of mpl.py.

#!/usr/bin/env python

import matplotlib
matplotlib.use( 'TkAgg')
import matplotlib.pyplot as plt
import numpy as np

plt.ion()

fig = plt.figure(1, figsize=(11.6,8.2))

sin = fig.add_subplot( 2, 1, 1, xlabel="Phase", ylabel="Sinus", title="Sin")
cos = fig.add_subplot( 2, 1, 2, xlabel="Phase", title="Cos")


t = np.arange( 0.0, 1.0, 0.01)
s = np.sin(2*np.pi*t)
c = np.cos(2*np.pi*t)

line_x, = sin.plot( t, s)
line_y, = cos.plot( t, c, 'r')

fig.canvas.flush_events()
#plt.pause( 3.0)
#plt.draw()

By using the 'from mpl import *' syntax the symbols of mpl.py are available on the IPython command line, e.g.:

In [2]: plt.get_fignums()
Out[2]: [1]

In [3]: fig.get_axes()
Out[3]: 
[<matplotlib.axes.AxesSubplot object at 0xa2c886c>,
 <matplotlib.axes.AxesSubplot object at 0xa2eaf4c>]