[Numpy-discussion] Nonblocking Plots with Matplotlib

Bill Baxter wbaxter@gmail....
Wed Mar 14 00:49:00 CDT 2007

Howdy Folks,

I was missing the good ole days of using Matlab back at the Uni when I
could debug my code, stop at breakpoints and plot various data without
fear of blocking the interpreter process.

Using "ipython -pylab" is what has been suggested to me in the past,
but the problem is I don't do my debugging from ipython.  I have a
very nice IDE that works very well, and it has a lovely interactive
debugging prompt that I can use to probe my code when stopped at a
breakpoint.  It's great except I can't really use matplotlib for
debugging there because it causes things to freeze up.

So I've come up with a decent (though not perfect) solution for
quickie interactive plots which is to run matplotlib in a separate
process.  I call the result it 'ezplot'.  The first alpha version of
this is now available at the Cheeseshop.  (I made an egg too, so if
you have setuptools you can do "easy_install ezplot".)

The basic usage is like so:

 In [1]: import ezplot
 In [2]: p = ezplot.Plotter()
 In [3]: p.plot([1,2,3],[1,4,9],marker='o')
 Connecting to server... waiting...
 connected to plotserver 0.1.0a1 on http://localhost:8397
 Out[3]: True
 In [4]: from numpy import *
 In [5]: x = linspace(-5,5,20)
 In [13]: p.clf()
 Out[13]: True
 In [14]: p.plot(x, x*x*log(x*x+0.01))

(Imagine lovely plots popping up on your screen as these commands are typed.)

The only return values you get back are True (success...probably) or
False (failure...for sure).  So no fancy plot object manipulation is
possible.  But you can do basic plots no problem.

The nice part is that this (unlike ipython's built-in -pylab threading
mojo) should work just as well from wherever you're using python.
Whether it's ipython (no -pylab) or Idle, or a plain MS-DOS console,
or WingIDE's debug probe, or SPE, or a PyCrust shell or whatever.  It
doesn't matter because all the client is doing is packing up data and
shipping over a socket.  All the GUI plotting mojo happens in a
completely separate process.

There are plenty of ways this could be made better, but for me, for
now, this probably does pretty much all I need, so it's back to Real
Work.  But if anyone is interested in making improvements to this, let
me know.

Here's a short list of things that could be improved:
* Right now I assume use of the wxAGG backend for matplotlib.  Don't
know how much work it would be to support other back ends (or how to
go about it, really).   wxAGG is what I always use.
* Returning more error/exception info from the server would be nice
* Returning full fledged proxy plot objects would be nice too, but I
suspect that's a huge effort
* SOAP may be better for this than xmlrpclib but I just couldn't get
it to work (SOAPpy + Twisted).
* A little more safety would be nice.  Anyone know how to make a
Twisted xmlrpc server not accept connections from anywhere except
* There's a little glitch in that the spawned plot server dies with
the parent that created it.  Maybe there's a flag to subprocess.Popen
to fix that?
* Sometimes when you click on "Exit Server", if there are plot windows
open it hangs while shutting down.

Only tested on Win32 but there's nothing much platform specific in there.

Give it a try and let me know what you think!


More information about the Numpy-discussion mailing list