from plotnine import *
from plotnine.data import mpg, penguins
Facets (subplots)
Facets split a plot into multiple subplots, based on one or more variables. facet_wrap()
creates a sequence of subplots, while facet_grid()
creates a matrix of subplots.
- How to create a sequence or matrix of subplots.
- How to use a special facetting syntax to define subplots.
- How to configure the x- and y-axis of subplots.
facet_wrap()
creates a sequence of subplots.- The
ncol
ornrow
arguments tofacet_wrap()
control the number of columns or rows. facet_grid()
creates a matrix of subplots based on two or more variables.- The
scales
argument can set a combination of thex
andy
axis to fixed for free.
Setup
Here is a single big plot that you might want to split into subplots.
("displ", "hwy", color="class")) + geom_point()
ggplot(mpg, aes( )
facet_wrap(): subplot sequence
Use facet_wrap()
to create a sequence of subplots. It accepts as its first argument the name of the column that should be used to split the data for subplots.
("displ", "hwy", color="class"))
ggplot(mpg, aes(+ geom_point(show_legend=False)
+ facet_wrap("class")
)
Note that by default facet_wrap()
fills row-by-row, with defaults for the number of subplots per row and column. Use either the ncol=
or nrow=
argument to fix the number of plots per column or row, respectively.
("displ", "hwy", color="class"))
ggplot(mpg, aes(+ geom_point(show_legend=False)
+ facet_wrap("class", ncol=2)
)
facet_grid(): subplot matrix
Use facet_grid()
to create a matrix of subplots. It accepts two column names as arguments, the first for the rows and the second for the columns.
"displ", "hwy")) + geom_point() + facet_grid("cyl", "year")) (ggplot(mpg, aes(
facetting syntax
Both facet_wrap()
and facet_grid()
support a special syntax for defining subplots. It takes the form "var1 ~ var2 + var3"
"displ", "hwy")) + geom_point() + facet_grid("cyl ~ year")) (ggplot(mpg, aes(
Notice that cyl
values (e.g. 4, 5, 6, 8) are on the rows, while year
values are on the columns.
The column names to the left of the tilde (~
) define subplot rows, while those to the right define subplot columns. The plus sign (+
) groups variables for creating subplots (TODO: is there a reasonable example that includes +
?).
scales=
for freeing axes
By default, the x- and y-axes of each subplot have the same range. Use the scales=
argument to allow each row or column to have its own range.
("displ", "hwy"))
ggplot(mpg, aes(+ geom_point()
+ facet_grid("cyl ~ year", scales="free_y")
)