plotnine.geoms.geom.geom

geom(mapping=None, data=None, **kwargs)

Base class of all Geoms

Attributes

Name Description
DEFAULT_AES dict() -> new empty dictionary
DEFAULT_PARAMS dict() -> new empty dictionary
NON_MISSING_AES set() -> new empty set object
REQUIRED_AES set() -> new empty set object
aes_params dict() -> new empty dictionary
data Geom/layer specific dataframe
legend_geom str(object=’’) -> str
mapping Mappings i.e. aes(x="col1", fill="col2")

DEFAULT_AES

DEFAULT_AES : dict[str, Any] = {}

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)

DEFAULT_PARAMS

DEFAULT_PARAMS : dict[str, Any] = {}

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)

NON_MISSING_AES

NON_MISSING_AES : set[str] = set()

set() -> new empty set object set(iterable) -> new set object

Build an unordered collection of unique elements.

REQUIRED_AES

REQUIRED_AES : set[str] = set()

set() -> new empty set object set(iterable) -> new set object

Build an unordered collection of unique elements.

aes_params

aes_params : dict[str, Any] = copy_keys(kwargs, {}, self.aesthetics())

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)

data

data : DataLike = kwargs["data"]

Geom/layer specific dataframe

legend_geom

legend_geom : str = "point"

str(object=’’) -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.__str__() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to ‘strict’.

mapping

mapping : aes = kwargs["mapping"]

Mappings i.e. aes(x="col1", fill="col2")

Methods

Name Description
aesthetics Return all the aesthetics for this geom
draw_group Plot data belonging to a group.
draw_layer Draw layer across all panels
draw_legend Draw a rectangle in the box
draw_panel Plot all groups
draw_unit Plot data belonging to a unit.
from_stat Return an instantiated geom object
handle_na Remove rows with NaN values
legend_key_size Calculate the size of key that would fit the layer contents
setup_data Modify the data before drawing takes place
to_layer Make a layer that represents this geom
use_defaults Combine data with defaults and set aesthetics from parameters

aesthetics

aesthetics()

Return all the aesthetics for this geom

geoms should not override this method.

draw_group

draw_group(data, panel_params, coord, ax, **params)

Plot data belonging to a group.

Parameters

data : pd.DataFrame

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

panel_params : panel_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
coord : coord

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

ax : axes

Axes on which to plot.

params : dict = {}

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

draw_layer

draw_layer(data, layout, coord, **params)

Draw layer across all panels

geoms should not override this method.

Parameters

data : pd.DataFrame

DataFrame specific for this layer

layout : Layout

Layout object created when the plot is getting built

coord : coord

Type of coordinate axes

params : Any = {}

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

draw_legend

draw_legend(data, da, lyr)

Draw a rectangle in the box

Parameters

data : pd.Series[Any]

A row of the data plotted to this layer

da : DrawingArea

Canvas on which to draw

lyr : layer

Layer that the geom belongs to.

Returns

DrawingArea

The DrawingArea after a layer has been drawn onto it.

draw_panel

draw_panel(data, panel_params, coord, ax, **params)

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

data : pd.DataFrame

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

panel_params : panel_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
coord : coord

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

ax : Axes

Axes on which to plot.

params : Any = {}

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

draw_unit

draw_unit(data, panel_params, coord, ax, **params)

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

data : pd.DataFrame

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

panel_params : panel_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
coord : coord

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

ax : Axes

Axes on which to plot.

params : Any = {}

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

from_stat

from_stat(stat)

Return an instantiated geom object

geoms should not override this method.

Parameters

stat : stat

stat

Returns

geom

A geom object

Raises

PlotnineError

If unable to create a geom.

handle_na

handle_na(data)

Remove rows with NaN values

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

Parameters

data : pd.DataFrame

Data

Returns

pd.DataFrame

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.

legend_key_size

legend_key_size(data, min_size, lyr)

Calculate the size of key that would fit the layer contents

Parameters

data : pd.Series[Any]

A row of the data plotted to this layer

min_size : TupleInt2

Initial size which should be expanded to fit the contents.

lyr : layer

Layer

setup_data

setup_data(data)

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

data : pd.DataFrame

Data used for drawing the geom.

Returns

pd.DataFrame

Data used for drawing the geom.

to_layer

to_layer()

Make a layer that represents this geom

Returns

layer

Layer

use_defaults

use_defaults(data, aes_modifiers)

Combine data with defaults and set aesthetics from parameters

geoms should not override this method.

Parameters

data : pd.DataFrame

Data used for drawing the geom.

aes_modifiers : dict[str, Any]

Aesthetics

Returns

pd.DataFrame

Data used for drawing the geom.