plotnine.composition.Beside

Beside(items)

Place plots or compositions side by side

Usage

plot | plot
plot | composition
composition | plot
composition | composition

Typically, you will use this class through the | operator.

Parameter Attributes

items: list[ggplot | Compose]

The objects to be arranged (composed).

See Also

Stack

To arrange plots vertically

plot_spacer

To add a blank space between plots

Compose

For more on composing plots

Examples

from plotnine import (
    ggplot,
    aes,
    geom_boxplot,
    geom_violin,
    geom_sina,
    labs,
    element_blank,
    theme,
    theme_xkcd,
    theme_538,
)
from plotnine.composition import Beside
from plotnine.data import mpg
p = ggplot(mpg, aes("factor(year)", "hwy")) + labs(x="year")

p1 = p + geom_boxplot()
p2 = p + geom_violin()
p3 = p + geom_sina()

p1 | p2 | p3

Which is equivalent to

Beside([p1, p2, p3])

Remove unnecessary breaks and texts.

no_y = theme(
    axis_title_y=element_blank(),
    axis_text_y=element_blank(),
    axis_ticks_y=element_blank()
)
no_title_x = theme(axis_title_x=element_blank())

(p1 + no_title_x) | (p2 + no_y) | (p3 + no_y + no_title_x)

Each plot in the composition can have a completely different theme.

(p1 + theme_xkcd() + no_title_x) | (p2 + no_y) | (p3 + theme_538() + no_y + no_title_x)

The space taken up by the plots and “sub-compositions” is distributed equally with in the group.

p1 | (p2 | p3)

Which is the same as:

Beside([p1, Beside([p2, p3])])
Source: Beside.ipynb