Skip to content

Commit f1ae145

Browse files
committed
Merge remote-tracking branch 'upstream/v1.3.x'
2 parents 803e538 + eae31b1 commit f1ae145

File tree

4 files changed

+42
-67
lines changed

4 files changed

+42
-67
lines changed

doc/api/index_backend_api.rst

+1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ backends
99
backend_qt4agg_api.rst
1010
backend_wxagg_api.rst
1111
backend_pdf_api.rst
12+
backend_webagg.rst
1213
dviread.rst
1314
type1font.rst

examples/pylab_examples/multipage_pdf.py

+30-33
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

+11-8
Original file line numberDiff line numberDiff line change
@@ -2252,15 +2252,12 @@ class PdfPages(object):
22522252
Use like this::
22532253
22542254
# Initialize:
2255-
pp = PdfPages('foo.pdf')
2255+
with PdfPages('foo.pdf') as pdf:
22562256
2257-
# As many times as you like, create a figure fig, then either:
2258-
fig.savefig(pp, format='pdf') # note the format argument!
2259-
# or:
2260-
pp.savefig(fig)
2261-
2262-
# Once you are done, remember to close the object:
2263-
pp.close()
2257+
# As many times as you like, create a figure fig and save it:
2258+
# When no figure is specified the current figure is saved
2259+
pdf.savefig(fig)
2260+
pdf.savefig()
22642261
22652262
(In reality PdfPages is a thin wrapper around PdfFile, in order to
22662263
avoid confusion when using savefig and forgetting the format
@@ -2276,6 +2273,12 @@ def __init__(self, filename):
22762273
"""
22772274
self._file = PdfFile(filename)
22782275

2276+
def __enter__(self):
2277+
return self
2278+
2279+
def __exit__(self, exc_type, exc_val, exc_tb):
2280+
self.close()
2281+
22792282
def close(self):
22802283
"""
22812284
Finalize this object, making the underlying file a complete

lib/matplotlib/testing/compare.py

-26
Original file line numberDiff line numberDiff line change
@@ -309,32 +309,6 @@ def compare_images( expected, actual, tol, in_decorator=False ):
309309
expected_version = version.LooseVersion("1.6")
310310
found_version = version.LooseVersion(np.__version__)
311311

312-
# On Numpy 1.6, we can use bincount with minlength, which is much faster than
313-
# using histogram
314-
if found_version >= expected_version:
315-
rms = 0
316-
317-
for i in xrange(0, 3):
318-
h1p = expectedImage[:,:,i]
319-
h2p = actualImage[:,:,i]
320-
321-
h1h = np.bincount(h1p.ravel(), minlength=256)
322-
h2h = np.bincount(h2p.ravel(), minlength=256)
323-
324-
rms += np.sum(np.power((h1h-h2h), 2))
325-
else:
326-
rms = 0
327-
ns = np.arange(257)
328-
329-
for i in xrange(0, 3):
330-
h1p = expectedImage[:,:,i]
331-
h2p = actualImage[:,:,i]
332-
333-
h1h = np.histogram(h1p, bins=ns)[0]
334-
h2h = np.histogram(h2p, bins=ns)[0]
335-
336-
rms += np.sum(np.power((h1h-h2h), 2))
337-
338312
rms = calculate_rms(expectedImage, actualImage)
339313

340314
diff_image = make_test_filename(actual, 'failed-diff')

0 commit comments

Comments
 (0)