seaborn.
kdeplot
(data, data2=None, shade=False, vertical=False, kernel='gau', bw='scott', gridsize=100, cut=3, clip=None, legend=True, cumulative=False, shade_lowest=True, cbar=False, cbar_ax=None, cbar_kws=None, ax=None, **kwargs)¶Fit and plot a univariate or bivariate kernel density estimate.
Parameters: | data : 1d array-like
data2: 1d array-like, optional
shade : bool, optional
vertical : bool, optional
kernel : {‘gau’ | ‘cos’ | ‘biw’ | ‘epa’ | ‘tri’ | ‘triw’ }, optional
bw : {‘scott’ | ‘silverman’ | scalar | pair of scalars }, optional
gridsize : int, optional
cut : scalar, optional
clip : pair of scalars, or pair of pair of scalars, optional
legend : bool, optional
cumulative : bool, optional
shade_lowest : bool, optional
cbar : bool, optional
cbar_ax : matplotlib axes, optional
cbar_kws : dict, optional
ax : matplotlib axes, optional
kwargs : key, value pairings
|
---|---|
Returns: | ax : matplotlib Axes
|
See also
Examples
Plot a basic univariate density:
>>> import numpy as np; np.random.seed(10)
>>> import seaborn as sns; sns.set(color_codes=True)
>>> mean, cov = [0, 2], [(1, .5), (.5, 1)]
>>> x, y = np.random.multivariate_normal(mean, cov, size=50).T
>>> ax = sns.kdeplot(x)
Shade under the density curve and use a different color:
>>> ax = sns.kdeplot(x, shade=True, color="r")
Plot a bivariate density:
>>> ax = sns.kdeplot(x, y)
Use filled contours:
>>> ax = sns.kdeplot(x, y, shade=True)
Use more contour levels and a different color palette:
>>> ax = sns.kdeplot(x, y, n_levels=30, cmap="Purples_d")
Use a narrower bandwith:
>>> ax = sns.kdeplot(x, bw=.15)
Plot the density on the vertical axis:
>>> ax = sns.kdeplot(y, vertical=True)
Limit the density curve within the range of the data:
>>> ax = sns.kdeplot(x, cut=0)
Add a colorbar for the contours:
>>> ax = sns.kdeplot(x, y, cbar=True)
Plot two shaded bivariate densities:
>>> iris = sns.load_dataset("iris")
>>> setosa = iris.loc[iris.species == "setosa"]
>>> virginica = iris.loc[iris.species == "virginica"]
>>> ax = sns.kdeplot(setosa.sepal_width, setosa.sepal_length,
... cmap="Reds", shade=True, shade_lowest=False)
>>> ax = sns.kdeplot(virginica.sepal_width, virginica.sepal_length,
... cmap="Blues", shade=True, shade_lowest=False)