plotnine.ggplot.save_as_pdf_pages

plotnine.ggplot.save_as_pdf_pages(plots, filename=None, path=None, verbose=True, **kwargs)[source]

Save multiple ggplot objects to a PDF file, one per page.

Parameters
plotscollection or generator of ggplot

Plot objects to write to file. plots may be either a collection such as a list or set:

>>> base_plot = ggplot(…)
>>> plots = [base_plot + ggtitle('%d of 3' % i) for i in range(1, 3)]
>>> save_as_pdf_pages(plots)

or, a generator that yields ggplot objects:

>>> def myplots():
>>>     for i in range(1, 3):
>>>         yield ggplot(…) + ggtitle('%d of 3' % i)
>>> save_as_pdf_pages(myplots())
filenamestr, optional

File name to write the plot to. If not specified, a name like “plotnine-save-<hash>.pdf” is used.

pathstr, optional

Path to save plot to (if you just want to set path and not filename).

verbosebool

If True, print the saving information.

kwargsdict

Additional arguments to pass to matplotlib.figure.Figure.savefig().

Notes

Using pandas' groupby() methods, tidy data can be “faceted” across pages:

>>> from plotnine.data import mtcars
>>> def facet_pages(column)
>>>     base_plot = [
>>>         aes(x='wt', y='mpg', label='name'),
>>>         geom_text(),
>>>         ]
>>>     for label, group_data in mtcars.groupby(column):
>>>         yield ggplot(group_data) + base_plot + ggtitle(label)
>>> save_as_pdf_pages(facet_pages('cyl'))

Unlike ggplot.save(), save_as_pdf_pages() does not process arguments for height or width. To set the figure size, add figure_size to the theme for some or all of the objects in plots:

>>> plot = ggplot(…)
>>> # The following are equivalent
>>> plot.save('filename.pdf', height=6, width=8)
>>> save_as_pdf_pages([plot + theme(figure_size=(8, 6))])