Skip to content

Commit 303477a

Browse files
committed
only show preferred usage of PdfPages in examples
1 parent 8b69b80 commit 303477a

File tree

2 files changed

+29
-57
lines changed

2 files changed

+29
-57
lines changed

examples/pylab_examples/multipage_pdf.py

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,35 @@
77
from pylab import *
88

99
# Create the PdfPages object to which we will save the pages:
10-
pdf = PdfPages('multipage_pdf.pdf')
11-
12-
figure(figsize=(3,3))
13-
plot(range(7), [3,1,4,1,5,9,2], 'r-o')
14-
title('Page One')
15-
savefig(pdf, format='pdf') # note the format='pdf' argument!
16-
close()
17-
18-
rc('text', usetex=True)
19-
figure(figsize=(8,6))
20-
x = np.arange(0,5,0.1)
21-
plot(x, np.sin(x), 'b-')
22-
title('Page Two')
23-
pdf.savefig() # here's another way - or you could do pdf.savefig(1)
24-
close()
25-
26-
rc('text', usetex=False)
27-
fig=figure(figsize=(4,5))
28-
plot(x, x*x, 'ko')
29-
title('Page Three')
30-
pdf.savefig(fig) # or you can pass a Figure object to pdf.savefig
31-
close()
32-
33-
# We can also set the file's metadata via the PdfPages object:
34-
d = pdf.infodict()
35-
d['Title'] = 'Multipage PDF Example'
36-
d['Author'] = u'Jouni K. Sepp\xe4nen'
37-
d['Subject'] = 'How to create a multipage pdf file and set its metadata'
38-
d['Keywords'] = 'PdfPages multipage keywords author title subject'
39-
d['CreationDate'] = datetime.datetime(2009,11,13)
40-
d['ModDate'] = datetime.datetime.today()
41-
42-
# Remember to close the object - otherwise the file will not be usable
43-
pdf.close()
44-
45-
# Or use the with statement, the file gets properly closed at the end:
46-
with PdfPages('multipage_pdf2.pdf') as pdf:
47-
48-
figure(figsize=(3, 3))
49-
plot(range(7), [3, 1, 4, 1, 5, 9, 2], 'r-o')
10+
# The with statement makes sure that the PdfPages object is closed properly at
11+
# the end of the block, even if an Exception occurs.
12+
with PdfPages('multipage_pdf.pdf') as pdf:
13+
figure(figsize=(3,3))
14+
plot(range(7), [3,1,4,1,5,9,2], 'r-o')
5015
title('Page One')
51-
pdf.savefig()
16+
pdf.savefig() # saves the current figure into a pdf page
5217
close()
5318

5419
rc('text', usetex=True)
55-
figure(figsize=(8, 6))
56-
x = np.arange(0, 5, 0.1)
20+
figure(figsize=(8,6))
21+
x = np.arange(0,5,0.1)
5722
plot(x, np.sin(x), 'b-')
5823
title('Page Two')
5924
pdf.savefig()
6025
close()
26+
27+
rc('text', usetex=False)
28+
fig=figure(figsize=(4,5))
29+
plot(x, x*x, 'ko')
30+
title('Page Three')
31+
pdf.savefig(fig) # or you can pass a Figure object to pdf.savefig
32+
close()
33+
34+
# We can also set the file's metadata via the PdfPages object:
35+
d = pdf.infodict()
36+
d['Title'] = 'Multipage PDF Example'
37+
d['Author'] = u'Jouni K. Sepp\xe4nen'
38+
d['Subject'] = 'How to create a multipage pdf file and set its metadata'
39+
d['Keywords'] = 'PdfPages multipage keywords author title subject'
40+
d['CreationDate'] = datetime.datetime(2009,11,13)
41+
d['ModDate'] = datetime.datetime.today()

lib/matplotlib/backends/backend_pdf.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2248,21 +2248,12 @@ class PdfPages(object):
22482248
Use like this::
22492249
22502250
# Initialize:
2251-
pp = PdfPages('foo.pdf')
2251+
with PdfPages('foo.pdf') as pdf:
22522252
2253-
# As many times as you like, create a figure fig, then either:
2254-
fig.savefig(pp, format='pdf') # note the format argument!
2255-
# or:
2256-
pp.savefig(fig)
2257-
2258-
# Once you are done, remember to close the object:
2259-
pp.close()
2260-
2261-
Or using the with statement::
2262-
2263-
with PdfPages('foo.pdf') as pp:
2264-
fig.savefig(pp, format='pdf') # note the format argument!
2265-
pp.savefig(fig)
2253+
# As many times as you like, create a figure fig and save it:
2254+
# When no figure is specified the current figure is saved
2255+
pdf.savefig(fig)
2256+
pdf.savefig()
22662257
22672258
(In reality PdfPages is a thin wrapper around PdfFile, in order to
22682259
avoid confusion when using savefig and forgetting the format

0 commit comments

Comments
 (0)