Wednesday, 15 February 2012

python - What limits does the matplotlib backend place on rendering formats? -



python - What limits does the matplotlib backend place on rendering formats? -

i'm confused role played backend used matplotlib in determining formats can rendered.

for example, documentation says 'agg' backend generates png, "raster graphics" if

import matplotlib matplotlib.use(‘agg’) import matplotlib.pyplot fig, ax = matplotlib.pyplot.subplots() #...

i can use

fig.savefig(“thefig.pdf”)

to generate pdf, or

fig.savefig(“thefig.svg”)

to produce vector graphics.

what role backend play in limiting formats , kinds of rendering (vector vs. raster) matplotlib can produce?

good question! agg backend produces raster graphics.

what's happening when phone call fig.savefig('name.pdf') (or .svg) backend temporarily changed generate vector output.

each backend can take how handles this, agg, when save vector output, does:

pdf = self.switch_backends(figurecanvaspdf) homecoming pdf.print_pdf(*args, **kwargs)

similarly, pdf backend, temporarily switches agg if asked save raster image.

switching backends can done on fly non-interactive backends, technique used extensively "behind-the-scenes" allow figures saved in multiple formats.

a bit more detail, in case you're ever writing matplotlib backend: matplotlib canvas instance has several print_<format> methods:

in [24]: backend_bases.figurecanvasbase.print_<tab> backend_bases.figurecanvasbase.print_bmp backend_bases.figurecanvasbase.print_eps backend_bases.figurecanvasbase.print_figure backend_bases.figurecanvasbase.print_jpeg backend_bases.figurecanvasbase.print_jpg backend_bases.figurecanvasbase.print_pdf backend_bases.figurecanvasbase.print_pgf backend_bases.figurecanvasbase.print_png backend_bases.figurecanvasbase.print_ps backend_bases.figurecanvasbase.print_raw backend_bases.figurecanvasbase.print_rgba backend_bases.figurecanvasbase.print_svg backend_bases.figurecanvasbase.print_svgz backend_bases.figurecanvasbase.print_tif backend_bases.figurecanvasbase.print_tiff

the print_figure method controls saving in general. saving specific format handled looking approriate print_<formatname> method (see figurecanvasbase._get_print_method exact details).

each backend specifies formats can save overriding methods. default each temporarily switch relevant "base" backend (e.g. agg raster formats, pdf pdf, svg svg, etc). allows figures saved formats if backend you're using supports single format.

python matplotlib format rendering

No comments:

Post a Comment