plotnine.save_as_pdf_pages
=None, path=None, verbose=True, kwargs={}) save_as_pdf_pages(plots, filename
Save multiple ggplot
objects to a PDF file, one per page.
Parameters
plots : Iterable[ggplot]
-
Plot objects to write to file.
plots
may be either a collection such as alist
orset
= ggplot(…) base_plot = [base_plot + ggtitle('%d of 3' % i) for i in range(1, 3)] plots 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 : 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 ="wt", y="mpg", label="name"),
aes(x
geom_text(),
]for label, group_data in mtcars.groupby(column):
yield ggplot(group_data) + base_plot + ggtitle(label)
'cyl')) save_as_pdf_pages(facet_pages(
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
:
= ggplot(…)
plot # The following are equivalent
'filename.pdf', height=6, width=8)
plot.save(+ theme(figure_size=(8, 6))]) save_as_pdf_pages([plot