plotnine.save_as_pdf_pages
save_as_pdf_pages(plots, filename=None, path=None, verbose=True, **kwargs)Save multiple ggplot objects to a PDF file, one per page.
Parameters
plots : Iterable[ggplot]-
Plot objects to write to file.
plotsmay be either a collection such as alistorsetbase_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
ggplotobjects:def myplots(): for i in range(1, 3): yield ggplot(…) + ggtitle('%d of 3' % i) save_as_pdf_pages(myplots()) filename : Optional[str | Path] = None-
File name to write the plot to. If not specified, a name like “plotnine-save-
.pdf” is used. path : str | None = None-
Path to save plot to (if you just want to set path and not filename).
verbose : bool = True-
If
True, print the saving information. kwargs : Any-
Additional arguments to pass to
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 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))])