@@ -2251,29 +2251,44 @@ class PdfPages(object):
2251
2251
"""
2252
2252
A multi-page PDF file.
2253
2253
2254
- Use like this::
2255
-
2256
- # Initialize:
2257
- with PdfPages('foo.pdf') as pdf:
2258
-
2259
- # As many times as you like, create a figure fig and save it:
2260
- # When no figure is specified the current figure is saved
2261
- pdf.savefig(fig)
2262
- pdf.savefig()
2263
-
2264
- (In reality PdfPages is a thin wrapper around PdfFile, in order to
2265
- avoid confusion when using savefig and forgetting the format
2266
- argument.)
2254
+ Examples
2255
+ --------
2256
+
2257
+ >>> import matplotlib.pyplot as plt
2258
+ >>> # Initialize:
2259
+ >>> with PdfPages('foo.pdf') as pdf:
2260
+ ... # As many times as you like, create a figure fig and save it:
2261
+ ... fig = plt.figure()
2262
+ ... pdf.savefig(fig)
2263
+ ... # When no figure is specified the current figure is saved
2264
+ ... pdf.savefig()
2265
+
2266
+ Notes
2267
+ -----
2268
+
2269
+ In reality :class:`PdfPages` is a thin wrapper around :class:`PdfFile`, in
2270
+ order to avoid confusion when using :func:`~matplotlib.pyplot.savefig` and
2271
+ forgetting the format argument.
2267
2272
"""
2268
- __slots__ = ('_file' ,)
2273
+ __slots__ = ('_file' , 'keep_empty' )
2269
2274
2270
- def __init__ (self , filename ):
2275
+ def __init__ (self , filename , keep_empty = True ):
2271
2276
"""
2272
- Create a new PdfPages object that will be written to the file
2273
- named *filename*. The file is opened at once and any older
2274
- file with the same name is overwritten.
2277
+ Create a new PdfPages object.
2278
+
2279
+ Parameters
2280
+ ----------
2281
+
2282
+ filename: str
2283
+ Plots using :meth:`PdfPages.savefig` will be written to a file at
2284
+ this location. The file is opened at once and any older file with
2285
+ the same name is overwritten.
2286
+ keep_empty: bool, optional
2287
+ If set to False, then empty pdf files will be deleted automatically
2288
+ when closed.
2275
2289
"""
2276
2290
self ._file = PdfFile (filename )
2291
+ self .keep_empty = keep_empty
2277
2292
2278
2293
def __enter__ (self ):
2279
2294
return self
@@ -2287,6 +2302,8 @@ def close(self):
2287
2302
PDF file.
2288
2303
"""
2289
2304
self ._file .close ()
2305
+ if self .get_pagecount () == 0 and self .keep_empty is False :
2306
+ os .remove (self ._file .fh .name )
2290
2307
self ._file = None
2291
2308
2292
2309
def infodict (self ):
@@ -2299,10 +2316,19 @@ def infodict(self):
2299
2316
2300
2317
def savefig (self , figure = None , ** kwargs ):
2301
2318
"""
2302
- Save the Figure instance *figure* to this file as a new page.
2303
- If *figure* is a number, the figure instance is looked up by
2304
- number, and if *figure* is None, the active figure is saved.
2305
- Any other keyword arguments are passed to Figure.savefig.
2319
+ Saves a :class:`~matplotlib.figure.Figure` to this file as a new page.
2320
+
2321
+ Any other keyword arguments are passed to
2322
+ :meth:`~matplotlib.figure.Figure.savefig`.
2323
+
2324
+ Parameters
2325
+ ----------
2326
+
2327
+ figure: :class:`~matplotlib.figure.Figure` or int, optional
2328
+ Specifies what figure is saved to file. If not specified, the
2329
+ active figure is saved. If a :class:`~matplotlib.figure.Figure`
2330
+ instance is provided, this figure is saved. If an int is specified,
2331
+ the figure instance to save is looked up by number.
2306
2332
"""
2307
2333
if isinstance (figure , Figure ):
2308
2334
figure .savefig (self , format = 'pdf' , ** kwargs )
0 commit comments