Skip to content

Commit

Permalink
Merge pull request #2416 from megies/multipage_pdf_with_statement
Browse files Browse the repository at this point in the history
Multipage pdf with statement
  • Loading branch information
mdboom committed Sep 18, 2013
2 parents a482fc2 + c60dfdd commit eae31b1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 41 deletions.
63 changes: 30 additions & 33 deletions examples/pylab_examples/multipage_pdf.py
Expand Up @@ -2,42 +2,39 @@

import datetime
import numpy as np
import matplotlib
from matplotlib.backends.backend_pdf import PdfPages
from pylab import *
import matplotlib.pyplot as plt

# Create the PdfPages object to which we will save the pages:
pdf = PdfPages('multipage_pdf.pdf')
# The with statement makes sure that the PdfPages object is closed properly at
# the end of the block, even if an Exception occurs.
with PdfPages('multipage_pdf.pdf') as pdf:
plt.figure(figsize=(3, 3))
plt.plot(range(7), [3, 1, 4, 1, 5, 9, 2], 'r-o')
plt.title('Page One')
pdf.savefig() # saves the current figure into a pdf page
plt.close()

figure(figsize=(3,3))
plot(range(7), [3,1,4,1,5,9,2], 'r-o')
title('Page One')
savefig(pdf, format='pdf') # note the format='pdf' argument!
close()
plt.rc('text', usetex=True)
plt.figure(figsize=(8, 6))
x = np.arange(0, 5, 0.1)
plt.plot(x, np.sin(x), 'b-')
plt.title('Page Two')
pdf.savefig()
plt.close()

rc('text', usetex=True)
figure(figsize=(8,6))
x = np.arange(0,5,0.1)
plot(x, np.sin(x), 'b-')
title('Page Two')
pdf.savefig() # here's another way - or you could do pdf.savefig(1)
close()
plt.rc('text', usetex=False)
fig = plt.figure(figsize=(4, 5))
plt.plot(x, x*x, 'ko')
plt.title('Page Three')
pdf.savefig(fig) # or you can pass a Figure object to pdf.savefig
plt.close()

rc('text', usetex=False)
fig=figure(figsize=(4,5))
plot(x, x*x, 'ko')
title('Page Three')
pdf.savefig(fig) # or you can pass a Figure object to pdf.savefig
close()

# We can also set the file's metadata via the PdfPages object:
d = pdf.infodict()
d['Title'] = 'Multipage PDF Example'
d['Author'] = u'Jouni K. Sepp\xe4nen'
d['Subject'] = 'How to create a multipage pdf file and set its metadata'
d['Keywords'] = 'PdfPages multipage keywords author title subject'
d['CreationDate'] = datetime.datetime(2009,11,13)
d['ModDate'] = datetime.datetime.today()

# Remember to close the object - otherwise the file will not be usable
pdf.close()
# We can also set the file's metadata via the PdfPages object:
d = pdf.infodict()
d['Title'] = 'Multipage PDF Example'
d['Author'] = u'Jouni K. Sepp\xe4nen'
d['Subject'] = 'How to create a multipage pdf file and set its metadata'
d['Keywords'] = 'PdfPages multipage keywords author title subject'
d['CreationDate'] = datetime.datetime(2009, 11, 13)
d['ModDate'] = datetime.datetime.today()
19 changes: 11 additions & 8 deletions lib/matplotlib/backends/backend_pdf.py
Expand Up @@ -2248,15 +2248,12 @@ class PdfPages(object):
Use like this::
# Initialize:
pp = PdfPages('foo.pdf')
with PdfPages('foo.pdf') as pdf:
# As many times as you like, create a figure fig, then either:
fig.savefig(pp, format='pdf') # note the format argument!
# or:
pp.savefig(fig)
# Once you are done, remember to close the object:
pp.close()
# As many times as you like, create a figure fig and save it:
# When no figure is specified the current figure is saved
pdf.savefig(fig)
pdf.savefig()
(In reality PdfPages is a thin wrapper around PdfFile, in order to
avoid confusion when using savefig and forgetting the format
Expand All @@ -2272,6 +2269,12 @@ def __init__(self, filename):
"""
self._file = PdfFile(filename)

def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.close()

def close(self):
"""
Finalize this object, making the underlying file a complete
Expand Down

0 comments on commit eae31b1

Please sign in to comment.