plotnine.layer.layer

layer(
    geom=None,
    stat=None,
    *,
    mapping=None,
    data=None,
    position=None,
    inherit_aes=True,
    show_legend=None,
    raster=False,
    **kwargs
)

Layer

When a geom or stat is added to a ggplot object, it creates a single layer. This class is a representation of that layer.

Parameters

geom : geom | type[geom] | str | None = None

Geom used to draw this layer. Accepts an instance, a class, or a string name (e.g. "point").

stat : stat | type[stat] | str | None = None

Stat used for the statistical transformation of data in this layer. Accepts an instance, a class, or a string name. If None, the geom’s default stat is used.

mapping : aes | None = None

Aesthetic mappings.

data : LayerDataLike | None = None

Data plotted in this layer. If None, the data from the ggplot object will be used.

position : position | type[position] | str | None = None

Position adjustment for geometries in this layer. Accepts an instance, a class, or a string name. If None, the geom’s default position is used.

inherit_aes : bool = True

If True inherit from the aesthetic mappings of the ggplot object.

show_legend : bool | dict[str, bool] | None = None

Whether to make up and show a legend for the mappings of this layer. If None then an automatic/good choice is made.

raster : bool = False

If True, draw onto this layer a raster (bitmap) object even if the final image format is vector.

**kwargs : Any

Keyword arguments passed to the geom constructor when geom is a class or string.

Methods

Name Description
compute_aesthetics Return a dataframe where the columns match the aesthetic mappings
compute_position Compute the position of each geometric object
compute_statistic Compute & return statistics for this layer
draw Draw geom
finish_statistics Prepare/modify data for plotting
map_statistic Mapping aesthetics to computed statistics
setup Prepare layer for the plot building
setup_data Prepare/modify data for plotting
update_labels Update label data for the ggplot from the mappings in this layer
use_defaults Prepare/modify data for plotting

compute_aesthetics

compute_aesthetics(plot)

Return a dataframe where the columns match the aesthetic mappings

Transformations like ‘factor(cyl)’ and other expression evaluation are made in here

compute_position

compute_position(layout)

Compute the position of each geometric object

This is in concert with the other objects in the panel depending on the position class of the geom

compute_statistic

compute_statistic(layout)

Compute & return statistics for this layer

draw

draw(layout, coord)

Draw geom

Parameters

layout : Layout

Layout object created when the plot is getting built

coord : coord

Type of coordinate axes

finish_statistics

finish_statistics()

Prepare/modify data for plotting

map_statistic

map_statistic(plot)

Mapping aesthetics to computed statistics

setup

setup(plot)

Prepare layer for the plot building

Give the layer access to the data, mapping and environment

setup_data

setup_data()

Prepare/modify data for plotting

update_labels

update_labels(plot)

Update label data for the ggplot from the mappings in this layer

use_defaults

use_defaults(data, aes_modifiers, scales=None)

Prepare/modify data for plotting

Parameters

data : pd.DataFrame

Data

aes_modifiers : dict[str, Any]

Expression to evaluate and replace aesthetics in the data.