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)andfig.savefig(figname)– orNone, Nonefor 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.extwill be the default figname.
Returns: backend, figname
Examples:
For a single output figure,
opts.outputcould 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 usename='':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/pyplotshould not be imported before the explicit choice of backend withmatplotlib.use(backend)