'could not open display' errors plotting inside a CGI script

I know this matplotlib code works. I'm having trouble

    > running it in a CGI script. It appears some of modules
    > matplotlib imports need certain X Window permissions or
    > DISPLAY variable set?....

It looks like mpl is trying to load its default backend (eg GTKAgg)
and in CGI mode you'll likely want to use an image backend (eg Agg).
See the FAQ http://matplotlib.sourceforge.net/faq.html#APPSERVER and
links therein. In particular, you'll likely want to set the backend
with

  backend : Agg

in your matplotlib rc file -- http://matplotlib.sf.net/matplotlibrc
or else in your CGI script with the *use* directive , eg

  import matplotlib
  matplotlib.use('Agg')
  import pylab

matplotlib.use must be called *before* importing pylab, and overrides
any rc or command line backend settings.

JDH