geom_density2d ¶
geom_density2d(
mapping: FeatureSpec | None = None,
*,
data: PlotData | None = None,
stat: str | None = None,
position: Position | None = None,
show_legend: bool | None = None,
inherit_aes: bool | None = None,
manual_key: ManualKey | None = None,
sampling: Sampling | None = None,
tooltips: Tooltips | None = None,
kernel: str | None = None,
adjust: float | None = None,
bw: str | float | Sequence[float] | None = None,
n: int | Sequence[int] | None = None,
bins: int | None = None,
binwidth: float | None = None,
color_by: str | None = None,
na_rm: bool | None = None,
**other_args: Any,
) -> LayerSpec
Draw contour lines of a 2D kernel density estimate (stat="density2d").
The density is estimated on an n x n grid over the x and y ranges, then
contoured. The stat computes ..level.., the density of each line.
Parameters:
-
mapping(FeatureSpec, default:None) –Aesthetic mapping from
aes():x,y(required),color,alpha,size,linetype,weight. -
data(DataFrame or dict, default:None) –Layer data; inherits the plot data when None.
-
stat(str, default:None) –Statistical transformation, by name;
"density2d"when None. -
position(str or FeatureSpec, default:None) –Position adjustment: a name such as
"identity"or aposition_*()spec. -
show_legend(bool, default:None) –False hides this layer from the legend.
-
inherit_aes(bool, default:None) –False ignores the plot's default mapping for this layer.
-
manual_key(str or FeatureSpec, default:None) –This layer's key in a manual legend: a label, or
layer_key(). -
sampling(str or FeatureSpec, default:None) –lets_plot sampling; accepted and ignored, as ggrust draws every row.
-
tooltips(FeatureSpec or list of str, default:None) –Tooltip content:
layer_tooltips(), a list of column names, or"none"to disable tooltips (HTML output). -
kernel(str, default:"gaussian") –Smoothing kernel:
"gaussian","rectangular","triangular","biweight","epanechikov","optcosine"or"cosine". -
adjust(float, default:1) –Multiplies the bandwidth; above 1 smooths more.
-
bw(str or float or list of float, default:"nrd0") –Bandwidth: a rule (
"nrd0"or"nrd", per axis), one number for both axes, or[x, y]. -
n(int or list of int, default:100) –Grid points along x and y (2 to 999); one number is used for both axes.
-
bins(int, default:10) –Number of levels, evenly spaced over the z range.
-
binwidth(float, default:None) –Distance between levels; wins over
bins. -
color_by(str, default:None) –Aesthetic the
colorscale applies to ("color"or"fill"). -
na_rm(bool, default:None) –True removes rows with missing values silently; otherwise they are removed with a warning.
-
**other_args(Any) –Constant aesthetics such as
color="steelblue"orsize=1.
Returns:
-
LayerSpec–The layer.
Examples:
>>> from ggrust import *
>>> import numpy as np
>>> rng = np.random.default_rng(1)
>>> data = {"duration": rng.normal(3, 1, 300), "wait": rng.normal(70, 12, 300)}
>>> plot = ggplot(data, aes("duration", "wait")) + geom_point() + geom_density2d()