plotnine.layer.layer

class plotnine.layer.layer(geom: Geom, stat: Stat, *, mapping: aes, data: LayerDataLike | None, position: Position, inherit_aes: bool = True, show_legend: bool | None = None, raster: bool = False)[source]

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:
geomgeom, optional

geom to used to draw this layer.

statstat, optional

stat used for the statistical transformation of data in this layer

mappingaes, optional

Aesthetic mappings.

datadataframe, optional

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

positionposition, optional

Position object to adjust the geometries in this layer.

inherit_aesbool, optional

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

show_legendbool or None, optional

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

rasterbool, optional (default: False)

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

Notes

There is no benefit to manually creating a layer. You should always use a geom or stat.

static from_geom(geom: Geom) Layer[source]

Create a layer given a geom

Parameters:
geomgeom

geom from which a layer will be created

Returns:
outlayer

Layer that represents the specific geom.

setup(plot: Ggplot)[source]

Prepare layer for the plot building

Give the layer access to the data, mapping and environment

compute_aesthetics(plot: Ggplot)[source]

Return a dataframe where the columns match the aesthetic mappings

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

compute_statistic(layout: Layout)[source]

Compute & return statistics for this layer

map_statistic(plot: Ggplot)[source]

Mapping aesthetics to computed statistics

setup_data()[source]

Prepare/modify data for plotting

compute_position(layout: Layout)[source]

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

draw(layout: Layout, coord: Coord)[source]

Draw geom

Parameters:
layoutLayout

Layout object created when the plot is getting built

coordcoord

Type of coordinate axes

use_defaults(data: pd.DataFrame | None = None, aes_modifiers: dict[str, Any] | None = None) pd.DataFrame[source]

Prepare/modify data for plotting

Parameters:
datadataframe, optional

Data

aes_modifiersdict

Expression to evaluate and replace aesthetics in the data.

finish_statistics()[source]

Prepare/modify data for plotting