mpl - Matplotlib utilities

Warning

mpl imports matplotlib, but should not import pylab or pyplot.

inspec.mpl.get_backend(inname, backend=None, name='figure')[source]

Decipher inname as a matplotlib backend (pylab|png|eps|pdf|svg) or a filename with backend-specific extension. Return backend, figname – to be used in matplotlib.use(backend) and fig.savefig(figname) – or None, None for interactive display.

Parameters:
  • inname – Input figure name (‘name.ext’) or backend name (‘backend’)
  • backend – default backend. If no valid backend can be deciphered from inname, use backend as default name
  • name – default figure basename. If inname is actually a backend ext, name.ext will be the default figname.
Returns:

backend, figname

Examples:

For a single output figure, opts.output could be a backend name or a complete filename to override default outname:

import matplotlib as M
backend, figname = get_backend(opts.output, name='my_default_figname')
if backend:
    M.use(backend)
import matplotlib.pyplot as P     # Has to be *after* M.use
[...]
if backend:
    print "Saving plot in", figname
    fig.savefig(figname)
else:
    P.show()

To produce multiple figures with the same backend (given with option opts.backend), one can use name='':

import matplotlib as M
backend, figext = get_backend(opts.backend, name='')
if backend:
    M.use(backend)
import matplotlib.pyplot as P     # Has to be *after* M.use
[...]
if backend:
    fig1.savefig('my_figname_1' + figext)
    fig2.savefig('my_figname_2' + figext)
else:
    P.show()

Warning

pylab/pyplot should not be imported before the explicit choice of backend with matplotlib.use(backend)

inspec.mpl.save_figures(prefix='figure', ext='pdf', joint=True)[source]

Save active figures in files prefix_XX.ext; PDF can be joined in a single file.

Parameters:
  • prefix – figure prefix
  • ext – figure extension
  • joint (bool) – joint PDF figures into a single file