class plotnine.stats.stat_density_2d(*args, **kwargs)[source]

Compute 2D kernel density estimation


stat_density_2d(mapping=None, data=None, geom='density_2d',
                position='identity', na_rm=False, package='statsmodels', n=64,
                levels=5, kde_params=None, contour=True, **kwargs)

Only the mapping and data can be positional, the rest must be keyword arguments. **kwargs can be aesthetics (or parameters) used by the geom.

mapping : aes, optional

Aesthetic mappings created with aes(). If specified and inherit.aes=True, it is combined with the default mapping for the plot. You must supply mapping if there is no plot mapping.

Aesthetic Default value

The bold aesthetics are required.

Options for computed aesthetics

'level'     # density level of a contour
'density'   # Computed density at a point
'piece'     # Numeric id of a contour in a given group

level is only relevant when contours are computed. density is available only when no contours are computed. piece is largely irrelevant.

data : dataframe, optional

The data to be displayed in this layer. If None, the data from from the ggplot() call is used. If specified, it overrides the data from the ggplot() call.

geom : str or stat, optional (default: density_2d)

The statistical transformation to use on the data for this layer. If it is a string, it must be the registered and known to Plotnine.

position : str or position, optional (default: identity)

Position adjustment. If it is a string, it must be registered and known to Plotnine.

na_rm : bool, optional (default: False)

If False, removes missing values with a warning. If True silently removes missing values.

contour : bool

Whether to create contours of the 2d density estimate. Default is True.

n : int, optional(default: 64)

Number of equally spaced points at which the density is to be estimated. For efficient computation, it should be a power of two.

levels : int or array_like

Contour levels. If an integer, it specifies the maximum number of levels, if array_like it is the levels themselves. Default is 5.

package : str in ['statsmodels', 'scipy', 'sklearn']

Package whose kernel density estimation to use. Default is statsmodels.

kde_params : dict

Keyword arguments to pass on to the kde class.

See also

statsmodels.nonparametric.kde.KDEMultivariate, scipy.stats.gaussian_kde, sklearn.neighbors.KernelDensity