lets_plot.facet_wrap#

lets_plot.facet_wrap(facets, ncol=None, nrow=None, *, scales=None, order=1, format=None, dir='h', labwidth=None)#

Split data by one or more faceting variables. For each data subset creates a plot panel and lays out panels according to the ncol, nrow and dir settings.

Parameters:
facetsstr or list

One or more faceting variable names.

ncolint

Number of columns.

nrowint

Number of rows.

scalesstr

Specify whether scales are shared across all facets. ‘fixed’ - shared (the default), ‘free’ - vary across both rows and columns, ‘free_x’ or ‘free_y’ - vary across rows or columns respectively.

orderint or list, default=1

Specify ordering direction panels. 1 - ascending, -1 - descending, 0 - no ordering. When a list is given, then values in the list are positionally matched to variables in facets.

formatstr or list

Specify the format pattern for displaying faceting values. The format values are positionally matched to variables in facets.

dir{‘h’, ‘v’}, default=’h’

Direction: either ‘h’ for horizontal, or ‘v’ for vertical.

labwidthint or list

The maximum label length (in characters) before a line breaking is applied. If the original facet label already contains \n as a text separator, the line breaking is not applied.

Returns:
FeatureSpec

Facet wrap specification.

Notes

Format patterns in the format parameter can be just a number format (like ‘d’) or a string template where number format is surrounded by curly braces: “{d} cylinders”.

For example:

  • ‘.2f’ -> ‘12.45’,

  • ‘Score: {.2f}’ -> ‘Score: 12.45’,

  • ‘Score: {}’ -> ‘Score: 12.454789’.

For more info see https://lets-plot.org/python/pages/formats.html.

Examples

1import numpy as np
2from lets_plot import *
3LetsPlot.setup_html()
4n = 100
5np.random.seed(42)
6x = np.random.normal(size=n)
7group = np.random.choice(['a', 'b'], size=n)
8ggplot({'x': x, 'group': group}, aes(x='x')) + \
9    geom_histogram() + facet_wrap(facets='group')

 1import numpy as np
 2from lets_plot import *
 3LetsPlot.setup_html()
 4n = 1000
 5np.random.seed(42)
 6x = np.random.normal(size=n)
 7p = [1/6, 1/3, 1/2]
 8y = np.random.choice(p, size=n, p=p)
 9ggplot({'x': x, 'y': y}, aes(x='x')) + \
10    geom_histogram() + \
11    facet_wrap(facets='y', order=-1, ncol=2, dir='v', format='.2f')