plotnine.geoms.geom.geom

class plotnine.geoms.geom.geom(mapping: Aes | None = None, data: DataLike | None = None, **kwargs: Any)[source]

Base class of all Geoms

DEFAULT_AES: dict[str, Any] = {}

Default aesthetics for the geom

REQUIRED_AES: set[str] = {}

Required aesthetics for the geom

NON_MISSING_AES: set[str] = {}

Required aesthetics for the geom

DEFAULT_PARAMS: dict[str, Any] = {}

Required parameters for the geom

mapping: Aes

mappings i.e. aes(x='col1', fill='col2')

data: DataLike

geom/layer specific dataframe

static from_stat(stat: stat) geom[source]

Return an instantiated geom object

geoms should not override this method.

Parameters:
statstat

stat

Returns:
outgeom

A geom object

Raises:
PlotnineError

If unable to create a geom.

classmethod aesthetics() set[str][source]

Return all the aesthetics for this geom

geoms should not override this method.

setup_data(data: DataFrame) DataFrame[source]

Modify the data before drawing takes place

This function is called before position adjustments are done. It is used by geoms to create the final aesthetics used for drawing. The base class method does nothing, geoms can override this method for two reasons:

  1. The stat does not create all the aesthetics (usually position aesthetics) required for drawing the geom, but those aesthetics can be computed from the available data. For example geom_boxplot and geom_violin.

  2. The geom inherits from another geom (superclass) which does the drawing and the superclass requires certain aesthetics to be present in the data. For example geom_tile and geom_area.

Parameters:
datadataframe

Data used for drawing the geom.

Returns:
outdataframe

Data used for drawing the geom.

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

Combine data with defaults and set aesthetics from parameters

geoms should not override this method.

Parameters:
datadataframe

Data used for drawing the geom.

aes_modifiersdict

Aesthetics

Returns:
outdataframe

Data used for drawing the geom.

draw_layer(data: pd.DataFrame, layout: Layout, coord: Coord, **params: Any)[source]

Draw layer across all panels

geoms should not override this method.

Parameters:
dataDataFrame

DataFrame specific for this layer

layoutLayout

Layout object created when the plot is getting built

coordcoord

Type of coordinate axes

paramsdict

Combined geom and stat parameters. Also includes the stacking order of the layer in the plot (zorder)

draw_panel(data: pd.DataFrame, panel_params: panel_view, coord: Coord, ax: Axes, **params: Any)[source]

Plot all groups

For efficiency, geoms that do not need to partition different groups before plotting should override this method and avoid the groupby.

Parameters:
datadataframe

Data to be plotted by this geom. This is the dataframe created in the plot_build pipeline.

panel_paramspanel_view

The scale information as may be required by the axes. At this point, that information is about ranges, ticks and labels. Attributes are of interest to the geom are:

'panel_params.x.range'  # tuple
'panel_params.y.range'  # tuple
coordcoord

Coordinate (e.g. coord_cartesian) system of the geom.

axaxes

Axes on which to plot.

paramsdict

Combined parameters for the geom and stat. Also includes the 'zorder'.

static draw_group(data: pd.DataFrame, panel_params: panel_view, coord: Coord, ax: Axes, **params: Any)[source]

Plot data belonging to a group.

Parameters:
datadataframe

Data to be plotted by this geom. This is the dataframe created in the plot_build pipeline.

panel_paramspanel_view

The scale information as may be required by the axes. At this point, that information is about ranges, ticks and labels. Keys of interest to the geom are:

'x_range'  # tuple
'y_range'  # tuple
coordcoord

Coordinate (e.g. coord_cartesian) system of the geom.

axaxes

Axes on which to plot.

paramsdict

Combined parameters for the geom and stat. Also includes the 'zorder'.

static draw_unit(data: pd.DataFrame, panel_params: panel_view, coord: Coord, ax: Axes, **params: Any)[source]

Plot data belonging to a unit.

A matplotlib plot function may require that an aethestic have a single unique value. e.g. linestyle='dashed' and not linestyle=['dashed', 'dotted', ...]. A single call to such a function can only plot lines with the same linestyle. However, if the plot we want has more than one line with different linestyles, we need to group the lines with the same linestyle and plot them as one unit. In this case, draw_group calls this function to do the plotting. For an example see geom_point.

Parameters:
datadataframe

Data to be plotted by this geom. This is the dataframe created in the plot_build pipeline.

panel_paramspanel_view

The scale information as may be required by the axes. At this point, that information is about ranges, ticks and labels. Keys of interest to the geom are:

'x_range'  # tuple
'y_range'  # tuple

In rare cases a geom may need access to the x or y scales. Those are available at:

'scales'   # SimpleNamespace
coordcoord

Coordinate (e.g. coord_cartesian) system of the geom.

axaxes

Axes on which to plot.

paramsdict

Combined parameters for the geom and stat. Also includes the 'zorder'.

to_layer() Layer[source]

Make a layer that represents this geom

Returns:
outlayer

Layer

handle_na(data: DataFrame) DataFrame[source]

Remove rows with NaN values

geoms that infer extra information from missing values should override this method. For example geom_path.

Parameters:
datadataframe

Data

Returns:
outdataframe

Data without the NaNs.

Notes

Shows a warning if the any rows are removed and the na_rm parameter is False. It only takes into account the columns of the required aesthetics.

static draw_legend(data: pd.Series[Any], da: DrawingArea, lyr: Layer) DrawingArea[source]

Draw a rectangle in the box

Parameters:
dataSeries

Data Row

daDrawingArea

Canvas

lyrlayer

Layer

Returns:
outDrawingArea