Multipage pdf files

I added support to the pdf backend for multipage pdf files. The current
API (which I'm not entirely happy with) is that you create a PdfFile
object, plot your figures, saving each one to the PdfFile object, and
then close the object. The part I'm unhappy about is that because
PdfFile is a file-like object - it has a write method - you can
accidentally save your figure as a png into it and get a broken pdf
file. You have to specify format='pdf' to savefig to avoid this.

Here's an example:

from matplotlib.backends.backend_pdf import PdfFile

pdf = PdfFile('multipage_pdf.pdf')

figure(...)
# build your figure
savefig(pdf, format='pdf')

# repeat for all your figures

pdf.close()

···

--
Jouni K. Sepp�nen
http://www.iki.fi/jks

It's slightly hackish, but would it be possible to do an "isinstance" check in savefig, and if the first arg is a PdfFile, set "format" to "pdf" automatically, and if "format" is set to something else raise an exception? A little hackish because it doesn't necessarily scale to other formats easily, but it would prevent the user from shooting herself in the foot.

Mike

Jouni K. Sepp�nen wrote:

···

I added support to the pdf backend for multipage pdf files. The current
API (which I'm not entirely happy with) is that you create a PdfFile
object, plot your figures, saving each one to the PdfFile object, and
then close the object. The part I'm unhappy about is that because
PdfFile is a file-like object - it has a write method - you can
accidentally save your figure as a png into it and get a broken pdf
file. You have to specify format='pdf' to savefig to avoid this.

Here's an example:

from matplotlib.backends.backend_pdf import PdfFile

pdf = PdfFile('multipage_pdf.pdf')

figure(...)
# build your figure
savefig(pdf, format='pdf')

# repeat for all your figures

pdf.close()

Another (still slightly hacky) way might be to define an optional attribute of the file object. Something like this:

if hasattr( outputFile, "mplFormat" ):
   format = getattr( outputFile, "mplFormat" )

Then have set PdfFile.mplFormat to be "pdf". This is a little less hacky in that it doesn't depend on coding a specific class into savefig.

Ted

···

-----Original Message-----
From: Michael Droettboom [mailto:mdroe@…31…]
Sent: Friday, January 02, 2009 7:27 AM
To: matplotlib-devel@lists.sourceforge.net
Subject: Re: [matplotlib-devel] Multipage pdf files

It's slightly hackish, but would it be possible to do an "isinstance"
check in savefig, and if the first arg is a PdfFile, set "format" to
"pdf" automatically, and if "format" is set to something else raise an
exception? A little hackish because it doesn't necessarily scale to
other formats easily, but it would prevent the user from shooting
herself in the foot.

Mike

Jouni K. Seppänen wrote:
> I added support to the pdf backend for multipage pdf files. The
current
> API (which I'm not entirely happy with) is that you create a PdfFile
> object, plot your figures, saving each one to the PdfFile object, and
> then close the object. The part I'm unhappy about is that because
> PdfFile is a file-like object - it has a write method - you can
> accidentally save your figure as a png into it and get a broken pdf
> file. You have to specify format='pdf' to savefig to avoid this.
>
> Here's an example:
>
> from matplotlib.backends.backend_pdf import PdfFile
>
> pdf = PdfFile('multipage_pdf.pdf')
>
> figure(...)
> # build your figure
> savefig(pdf, format='pdf')
>
> # repeat for all your figures
>
> pdf.close()
>
>
>

-----------------------------------------------------------------------
-------
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

No virus found in this incoming message.
Checked by AVG - http://www.avg.com
Version: 8.0.176 / Virus Database: 270.10.2/1872 - Release Date:
1/2/2009 1:10 PM