plotnine.geoms.geom.geom

class plotnine.geoms.geom.geom(mapping=None, data=None, **kwargs)[source]

Base class of all Geoms

DEFAULT_AES = {}

Default aesthetics for the geom

REQUIRED_AES = {}

Required aesthetics for the geom

NON_MISSING_AES = {}

Required aesthetics for the geom

DEFAULT_PARAMS = {}

Required parameters for the geom

mapping = None

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

data = None

geom/layer specific dataframe

static from_stat(stat)[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()[source]

Return all the aesthetics for this geom

geoms should not override this method.

setup_data(data)[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)[source]

Combine data with defaults and set aesthetics from parameters

geoms should not override this method.

Parameters
datadataframe

Data used for drawing the geom.

Returns
outdataframe

Data used for drawing the geom.

draw_layer(data, layout, coord, **params)[source]

Draw layer across all panels

geoms should not override this method.

Parameters
dataDataFrame

DataFrame specific for this layer

layoutLanel

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, panel_params, coord, ax, **params)[source]

Plot all groups

For effeciency, 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_paramstypes.SimpleNamespace

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, panel_params, coord, ax, **params)[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_paramsdict

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, panel_params, coord, ax, **params)[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_paramsdict

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()[source]

Make a layer that represents this geom

Returns
outlayer

Layer

handle_na(data)[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.