plotnine.ggplot.save_as_pdf_pages¶
- plotnine.ggplot.save_as_pdf_pages(plots: Iterable[ggplot], filename: str | Path | None = None, path: str | None = None, verbose: bool = True, **kwargs: Any)[source]¶
Save multiple
ggplot
objects to a PDF file, one per page.- Parameters:
- plots
collection
or generatorof
ggplot
Plot objects to write to file. plots may be either a collection such as a
list
orset
:>>> 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())
- filename
str
, optional File name to write the plot to. If not specified, a name like “plotnine-save-<hash>.pdf” is used.
- path
str
, optional Path to save plot to (if you just want to set path and not filename).
- verbose
bool
If
True
, print the saving information.- kwargs
dict
Additional arguments to pass to
matplotlib.figure.Figure.savefig()
.
- plots
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, addfigure_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))])