Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 319a78e

Browse files
committed
Improve multipage pdf documentation
svn path=/trunk/matplotlib/; revision=7597
1 parent 19f883a commit 319a78e

5 files changed

Lines changed: 52 additions & 11 deletions

File tree

doc/api/backend_pdf_api.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
:mod:`matplotlib.backends.backend_pdf`
3+
======================================
4+
5+
.. automodule:: matplotlib.backends.backend_pdf
6+
:members:
7+
:show-inheritance:

doc/api/index_backend_api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ matplotlib backends
88
backend_gtkagg_api.rst
99
backend_qt4agg_api.rst
1010
backend_wxagg_api.rst
11+
backend_pdf_api.rst
1112
dviread.rst
1213
type1font.rst

doc/faq/howto_faq.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,35 @@ on individual elements, eg::
6868
ax.set_xlabel('volts', alpha=0.5)
6969

7070

71+
.. _howto-multipage:
72+
73+
Save multiple plots in one pdf file
74+
-----------------------------------
75+
76+
Many image file formats can only have one image per file, but some
77+
formats support multi-page files. Currently only the pdf backend has
78+
support for this. To make a multi-page pdf file, first initialize the
79+
file::
80+
81+
from matplotlib.backends.backend_pdf import PdfPages
82+
pp = PdfPages('multipage.pdf')
83+
84+
You can give the :class:`~matplotlib.backends.backend_pdf.PdfPages`
85+
object to :func:`~matplotlib.pyplot.savefig`, but you have to specify
86+
the format::
87+
88+
savefig(pp, format='pdf')
89+
90+
A simpler way is to call
91+
:meth:`PdfPages.savefig <matplotlib.backends.backend_pdf.PdfPages.savefig>`::
92+
93+
pp.savefig()
94+
95+
Finally, the multipage pdf object has to be closed::
96+
97+
pp.close()
98+
99+
71100
.. _howto-subplots-adjust:
72101

73102
Move the edge of an axes to make room for tick labels

lib/matplotlib/backends/backend_pdf.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ def _flush(self):
348348
self.compressobj = None
349349

350350
class PdfFile(object):
351-
"""PDF file with one page."""
351+
"""PDF file object."""
352352

353353
def __init__(self, filename):
354354
self.nextObject = 1 # next free object id
@@ -1923,18 +1923,18 @@ class PdfPages(object):
19231923
"""
19241924
A multi-page PDF file.
19251925
1926-
Use like this:
1926+
Use like this::
19271927
1928-
# Initialize:
1929-
pdf_pages = PdfPages('foo.pdf')
1928+
# Initialize:
1929+
pp = PdfPages('foo.pdf')
19301930
1931-
# As many times as you like, create a figure fig, then either:
1932-
fig.savefig(pdf_pages, format='pdf') # note the format argument!
1933-
# or:
1934-
pdf_pages.savefig(fig)
1931+
# As many times as you like, create a figure fig, then either:
1932+
fig.savefig(pp, format='pdf') # note the format argument!
1933+
# or:
1934+
pp.savefig(fig)
19351935
1936-
# Once you are done, remember to close the object:
1937-
pdf_pages.close()
1936+
# Once you are done, remember to close the object:
1937+
pp.close()
19381938
19391939
(In reality PdfPages is a thin wrapper around PdfFile, in order to
19401940
avoid confusion when using savefig and forgetting the format

lib/matplotlib/figure.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,10 +981,14 @@ def savefig(self, *args, **kwargs):
981981
Arguments:
982982
983983
*fname*:
984-
A string containing a path to a filename, or a Python file-like object.
984+
A string containing a path to a filename, or a Python file-like object,
985+
or possibly some backend-dependent object such as
986+
:class:`~matplotlib.backends.backend_pdf.PdfPages`.
985987
986988
If *format* is *None* and *fname* is a string, the output
987989
format is deduced from the extension of the filename.
990+
If *fname* is not a string, remember to specify *format* to
991+
ensure that the correct backend is used.
988992
989993
Keyword arguments:
990994

0 commit comments

Comments
 (0)