Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit eae31b1

Browse filesBrowse files
committed
Merge pull request #2416 from megies/multipage_pdf_with_statement
Multipage pdf with statement
2 parents a482fc2 + c60dfdd commit eae31b1
Copy full SHA for eae31b1

File tree

Expand file treeCollapse file tree

2 files changed

+41
-41
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+41
-41
lines changed

‎examples/pylab_examples/multipage_pdf.py

Copy file name to clipboardExpand all lines: examples/pylab_examples/multipage_pdf.py
+30-33Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,39 @@
22

33
import datetime
44
import numpy as np
5-
import matplotlib
65
from matplotlib.backends.backend_pdf import PdfPages
7-
from pylab import *
6+
import matplotlib.pyplot as plt
87

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

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()
18+
plt.rc('text', usetex=True)
19+
plt.figure(figsize=(8, 6))
20+
x = np.arange(0, 5, 0.1)
21+
plt.plot(x, np.sin(x), 'b-')
22+
plt.title('Page Two')
23+
pdf.savefig()
24+
plt.close()
1725

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()
26+
plt.rc('text', usetex=False)
27+
fig = plt.figure(figsize=(4, 5))
28+
plt.plot(x, x*x, 'ko')
29+
plt.title('Page Three')
30+
pdf.savefig(fig) # or you can pass a Figure object to pdf.savefig
31+
plt.close()
2532

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()
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()

‎lib/matplotlib/backends/backend_pdf.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_pdf.py
+11-8Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2248,15 +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()
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()
22602257
22612258
(In reality PdfPages is a thin wrapper around PdfFile, in order to
22622259
avoid confusion when using savefig and forgetting the format
@@ -2272,6 +2269,12 @@ def __init__(self, filename):
22722269
"""
22732270
self._file = PdfFile(filename)
22742271

2272+
def __enter__(self):
2273+
return self
2274+
2275+
def __exit__(self, exc_type, exc_val, exc_tb):
2276+
self.close()
2277+
22752278
def close(self):
22762279
"""
22772280
Finalize this object, making the underlying file a complete

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.