From 5cb28403e5dd6c07053bd9d05b62e5e3c89e881f Mon Sep 17 00:00:00 2001 From: Tobias Megies Date: Thu, 12 Sep 2013 15:22:27 +0200 Subject: [PATCH 1/4] add __enter__ and __exit__ to PdfPages --- lib/matplotlib/backends/backend_pdf.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index 80f9c39f931a..4d1fe17c2ecc 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -2258,6 +2258,12 @@ class PdfPages(object): # Once you are done, remember to close the object: pp.close() + Or using the with statement:: + + with PdfPages('foo.pdf') as pp: + fig.savefig(pp, format='pdf') # note the format argument! + pp.savefig(fig) + (In reality PdfPages is a thin wrapper around PdfFile, in order to avoid confusion when using savefig and forgetting the format argument.) @@ -2272,6 +2278,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 From 8b69b80a1113e92e3788dd630e6a2add93674d6d Mon Sep 17 00:00:00 2001 From: Tobias Megies Date: Thu, 12 Sep 2013 15:23:00 +0200 Subject: [PATCH 2/4] multipage pdf: show usage of with statement in example --- examples/pylab_examples/multipage_pdf.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/examples/pylab_examples/multipage_pdf.py b/examples/pylab_examples/multipage_pdf.py index 9f385da877c4..db37fc9c19cd 100644 --- a/examples/pylab_examples/multipage_pdf.py +++ b/examples/pylab_examples/multipage_pdf.py @@ -41,3 +41,20 @@ # Remember to close the object - otherwise the file will not be usable pdf.close() + +# Or use the with statement, the file gets properly closed at the end: +with PdfPages('multipage_pdf2.pdf') as pdf: + + figure(figsize=(3, 3)) + plot(range(7), [3, 1, 4, 1, 5, 9, 2], 'r-o') + title('Page One') + pdf.savefig() + 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() + close() From 303477a4f046c2e4ca83975cf3d3b2534f11b424 Mon Sep 17 00:00:00 2001 From: Tobias Megies Date: Wed, 18 Sep 2013 13:54:27 +0200 Subject: [PATCH 3/4] only show preferred usage of PdfPages in examples --- examples/pylab_examples/multipage_pdf.py | 67 +++++++++--------------- lib/matplotlib/backends/backend_pdf.py | 19 ++----- 2 files changed, 29 insertions(+), 57 deletions(-) diff --git a/examples/pylab_examples/multipage_pdf.py b/examples/pylab_examples/multipage_pdf.py index db37fc9c19cd..73fa96bc5a81 100644 --- a/examples/pylab_examples/multipage_pdf.py +++ b/examples/pylab_examples/multipage_pdf.py @@ -7,54 +7,35 @@ from pylab import * # Create the PdfPages object to which we will save the pages: -pdf = PdfPages('multipage_pdf.pdf') - -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() - -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() - -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() - -# Or use the with statement, the file gets properly closed at the end: -with PdfPages('multipage_pdf2.pdf') as pdf: - - figure(figsize=(3, 3)) - plot(range(7), [3, 1, 4, 1, 5, 9, 2], 'r-o') +# 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: + figure(figsize=(3,3)) + plot(range(7), [3,1,4,1,5,9,2], 'r-o') title('Page One') - pdf.savefig() + pdf.savefig() # saves the current figure into a pdf page close() rc('text', usetex=True) - figure(figsize=(8, 6)) - x = np.arange(0, 5, 0.1) + figure(figsize=(8,6)) + x = np.arange(0,5,0.1) plot(x, np.sin(x), 'b-') title('Page Two') pdf.savefig() 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() diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index 4d1fe17c2ecc..cd01fb33b15d 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -2248,21 +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() - - Or using the with statement:: - - with PdfPages('foo.pdf') as pp: - fig.savefig(pp, format='pdf') # note the format argument! - pp.savefig(fig) + # 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 From c60dfdd4758eb1533d8a1d501dbf22dca5f3cba7 Mon Sep 17 00:00:00 2001 From: Tobias Megies Date: Wed, 18 Sep 2013 14:06:59 +0200 Subject: [PATCH 4/4] show only preferred usage for PdfPages and use preferred pyplot import style --- examples/pylab_examples/multipage_pdf.py | 37 ++++++++++++------------ 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/examples/pylab_examples/multipage_pdf.py b/examples/pylab_examples/multipage_pdf.py index 73fa96bc5a81..2ccdfd2fe046 100644 --- a/examples/pylab_examples/multipage_pdf.py +++ b/examples/pylab_examples/multipage_pdf.py @@ -2,34 +2,33 @@ 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: # 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: - figure(figsize=(3,3)) - plot(range(7), [3,1,4,1,5,9,2], 'r-o') - title('Page One') + 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 - close() + 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') + 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() - close() + 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() + 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() # We can also set the file's metadata via the PdfPages object: d = pdf.infodict() @@ -37,5 +36,5 @@ 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['CreationDate'] = datetime.datetime(2009, 11, 13) d['ModDate'] = datetime.datetime.today()