Composition¶
These objects are plotnine’s own (plotnine.composition), re-exported by
plotnine_extra; they are not vendored.
Compose¶
- class plotnine_extra.Compose(items)[source]¶
Bases:
objectBase class for those that create plot compositions
As a user, you will never directly work with this class, except through the operators that it makes possible. The operators are of two kinds:
### 1. Composing Operators
The combine plots or compositions into a single composition. Both operands are either a plot or a composition.
/
- : Arrange operands side by side.
Powered by the subclass [](~plotnine.composition.Beside).
|
- : Arrange operands vertically.
Powered by the subclass [](~plotnine.composition.Stack).
-
- : Arrange operands side by side _and_ at the same nesting level.
Also powered by the subclass [](~plotnine.composition.Beside).
+
- : Arrange operands in a 2D grid.
Powered by the subclass [](~plotnine.composition.Wrap).
### 2. Plot Modifying Operators
The modify all or some of the plots in a composition. The left operand is a composition and the right operand is a _plotaddable_; any object that can be added to a ggplot object e.g. _geoms_, _stats_, _themes_, _facets_, … .
&
: Add right hand side to all plots in the composition.
*
- : Add right hand side to all plots in the top-most nesting
level of the composition.
+
: Add right hand side to the last plot in the composition.
See also
plotnine.composition.BesideTo arrange plots side by side
plotnine.composition.StackTo arrange plots vertically
plotnine.composition.WrapTo arrange in a grid
plotnine.composition.plot_spacerTo add a blank space between plots
- figure: p9Figure¶
- property layout: plot_layout¶
The plot_layout of this composition
- property annotation: plot_annotation¶
The plot_annotation of this composition
- property theme: plotnine.theme¶
Theme for this composition
This is the default theme plus combined with theme from the annotation.
- property last_plot: ggplot¶
Last plot added to the composition
- draw(*, show=False)[source]¶
Render the arranged plots
- Parameters:
show (bool) – Whether to show the plot.
- Returns:
Matplotlib figure
- Return type:
Figure
- save(filename, format=None, dpi=None, **kwargs)[source]¶
Save a composition as an image file
- Parameters:
filename (str | Path | BytesIO) – File name or buffer to write the composition to.
format (str | None) – Output format. An explicit value takes priority over the filename extension and the composition theme’s figure_format. If none is set, use Matplotlib’s default. The retina format saves a PNG at twice the requested DPI. A .png extension retains a retina theme preference; pass format=”png” for ordinary resolution.
dpi (int | None) – DPI for raster graphics. If None, use the composition theme’s dpi.
**kwargs – These are ignored. Here to “softly” match the API of ggplot.save().
Beside¶
- class plotnine_extra.Beside(items)[source]¶
Bases:
ComposePlace plots or compositions side by side
Usage
plot | plot plot | composition composition | plot composition | composition
Typically, you will use this class through the | operator.
See also
plotnine.composition.StackTo arrange plots vertically
plotnine.composition.WrapTo arrange plots in a grid
plotnine.composition.plot_spacerTo add a blank space between plots
plotnine.composition.ComposeFor more on composing plots
Stack¶
- class plotnine_extra.Stack(items)[source]¶
Bases:
ComposePlace plots or compositions on top of each other
Usage
plot / plot plot / composition composition / plot composition / composition
Typically, you will use this class through the / operator.
See also
plotnine.composition.BesideTo arrange plots side by side
plotnine.composition.WrapTo arrange plots in a grid
plotnine.composition.plot_spacerTo add a blank space between plots
plotnine.composition.ComposeFor more on composing plots
Wrap¶
- class plotnine_extra.Wrap(items)[source]¶
Bases:
ComposeWrap plots or compositions into a grid
Usage
plot + plot plot + composition composition + plot composition + composition
Typically, you will use this class through the + operator.
- Parameters:
See also
plotnine.composition.BesideTo arrange plots side by side
plotnine.composition.StackTo arrange plots vertically
plotnine.composition.plot_spacerTo add a blank space between plots
plotnine.composition.ComposeFor more on composing plots
plot_layout¶
plot_annotation¶
- plotnine_extra.plot_annotation(*, title=None, subtitle=None, caption=None, footer=None, theme=<factory>)[source]¶
Annotate a composition
This applies to only the top-level composition. When a composition with an annotation is added to larger composition, the annotation of the sub-composition becomes irrelevant.
- Parameters:
title (str | None)
subtitle (str | None)
caption (str | None)
footer (str | None)
theme (plotnine.theme)
- Return type:
None
plot_spacer¶
- plotnine_extra.plot_spacer(fill=None, alpha=None)[source]¶
Blank area as wide or as tall as a plot
- Parameters:
fill (str | tuple[float, float, float] | tuple[float, float, float, float] | None) –
Background color. The default is a transparent area, but it can be changed through this parameter.
The color can also be modified by adding a [](~plotnine.theme) and setting the [](~plotnine.themes.themeable.plot_background).
alpha (float | None) –
Opacity of the background fill, between 0 (transparent) and 1 (opaque). The default leaves the area transparent.
The opacity can also be modified by adding a [](~plotnine.theme) and setting the [](~plotnine.themes.themeable.plot_background).
See also
plotnine.composition.BesideTo arrange plots side by side
plotnine.composition.StackTo arrange plots vertically
plotnine.composition.ComposeFor more on composing plots
guide_area¶
- plotnine_extra.guide_area(fill=None, alpha=None)[source]¶
A grid cell that hosts collected guides
Used in a composition that collects guides with plot_layout(guides=”collect”) to route the merged legend into a cell of the grid instead of placing it on the side of the composition.
Renders empty (like [](~plotnine.composition.plot_spacer)) when no collection is in effect, no guides exist to collect, or another guide_area was selected first.
- Parameters:
See also
plotnine.composition.plot_spacerBlank cell with the same styling.
plotnine.composition.plot_layoutSet guides=”collect” to enable collection.
inset_element¶
- plotnine_extra.inset_element(obj, left, bottom, right, top, align_to='panel', on_top=True, anchor='center')[source]¶
Place a plot as an inset within another plot
By default the inset is rendered on top of the host (on_top=True). With on_top=False it is rendered behind the host’s panel and labels but above the host’s plot_background. Adding an inset_element to a composition attaches it to the most recently added plot in that composition.
- Parameters:
obj (ggplot | Compose | PILImage | np.ndarray | _InsetImage) –
The object to render as an inset. One of:
ggplot or Compose — full plot pipeline.
PIL.Image.Image or numpy.ndarray — raster image. The image is letterboxed inside the user’s bbox so its aspect ratio is preserved.
left (float) – Bounding box of the inset as fractional coordinates in the range [0, 1], relative to the host region selected by align_to. The bottom-left corner of that region is (0, 0) and the top-right is (1, 1).
bottom (float) – Bounding box of the inset as fractional coordinates in the range [0, 1], relative to the host region selected by align_to. The bottom-left corner of that region is (0, 0) and the top-right is (1, 1).
right (float) – Bounding box of the inset as fractional coordinates in the range [0, 1], relative to the host region selected by align_to. The bottom-left corner of that region is (0, 0) and the top-right is (1, 1).
top (float) – Bounding box of the inset as fractional coordinates in the range [0, 1], relative to the host region selected by align_to. The bottom-left corner of that region is (0, 0) and the top-right is (1, 1).
align_to (Literal['panel', 'plot', 'full', 'footer']) –
Which region of the host plot the bounding box is relative to:
”panel” — the data area only (default).
- ”plot” — the panel plus axes, labels, titles, captions
and legends
”full” — everything the host plot occupies plus plot margin
- ”footer” — the footer band at the bottom of the plot. Only
non-degenerate when the host has footer text (set with labs(footer=…)); without it the band has zero height, the inset is skipped, and a warning is issued. The band does not grow to fit a taller inset — the inset overflows beyond it instead.
on_top (bool) – When True (default) the inset paints above the host plot. When False, the inset paints between the host’s plot_background and the rest of the host (panel, titles, legends, …), so the host’s panel area covers the inset. Useful for backdrops, decorations, or branding that should look like part of the page rather than an overlay.
anchor (Anchor) – Where to anchor the image inside the user’s bbox when its aspect ratio doesn’t match. One of “center” (default), “top”, “top-right”, “right”, “bottom-right”, “bottom”, “bottom-left”, “left”, “top-left”, or a (h, v) tuple in [0, 1]² with h = 0 left / h = 1 right and v = 0 bottom / v = 1 top. Only meaningful for image insets; plot / composition insets fill the entire area by resizing without constraining the aspect ratio, so the anchor has no effect.
- Return type:
None
Notes
The inset shares the host’s figure, so the host theme controls figure_size, dpi, and figure_format. The bounding box and align_to area determine the inset’s canvas size.
For image insets, inset_element(…) + theme(…) draws a sibling rectangle around the image; only plot_background is honored today.
Examples
Composed with a host plot:
>>> p = ggplot(mtcars, aes("wt", "mpg")) + geom_point() >>> p + inset_element(p, 0.6, 0.6, 1, 1)
Image inset with a black border:
>>> from PIL import Image >>> p + ( ... inset_element(Image.open("logo.png"), 0.7, 0.7, 1, 1) ... + theme(plot_background=element_rect(color="black", size=1)) ... )