KDE con displot() —#

  • 0:00 min | Última modificación: Octubre 13, 2021 | [YouTube]

[1]:
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
[2]:
penguins = sns.load_dataset("penguins")

display(
    penguins.head(),
    penguins.tail(),
    penguins.size,
)
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex
0 Adelie Torgersen 39.1 18.7 181.0 3750.0 Male
1 Adelie Torgersen 39.5 17.4 186.0 3800.0 Female
2 Adelie Torgersen 40.3 18.0 195.0 3250.0 Female
3 Adelie Torgersen NaN NaN NaN NaN NaN
4 Adelie Torgersen 36.7 19.3 193.0 3450.0 Female
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex
339 Gentoo Biscoe NaN NaN NaN NaN NaN
340 Gentoo Biscoe 46.8 14.3 215.0 4850.0 Female
341 Gentoo Biscoe 50.4 15.7 222.0 5750.0 Male
342 Gentoo Biscoe 45.2 14.8 212.0 5200.0 Female
343 Gentoo Biscoe 49.9 16.1 213.0 5400.0 Male
2408
[3]:
#
# Estimación no paramétrica básica usando dissplot().
#
sns.displot(
    penguins,
    x="flipper_length_mm",
    kind="kde",
    color='tab:blue',
    fill=True,
    rug=True,
    cumulative=False,
)
plt.show()
../../_images/02_seaborn_notebooks_3-34_distributions_displot_kde_univariado_3_0.png
[4]:
#
# Modificación del ancho de banda para la estimación de los kernels.
#
sns.displot(
    penguins,
    x="flipper_length_mm",
    kind="kde",
    bw_adjust=0.25,
)
plt.show()
../../_images/02_seaborn_notebooks_3-34_distributions_displot_kde_univariado_4_0.png
[5]:
#
# Compare el resultado con el gráfico anterior.
#
sns.displot(
    penguins,
    x="flipper_length_mm",
    kind="kde",
    bw_adjust=2,
)
plt.show()
../../_images/02_seaborn_notebooks_3-34_distributions_displot_kde_univariado_5_0.png
[6]:
#
# kde por variable categórica.
#
sns.displot(
    penguins,
    x="flipper_length_mm",
    hue="species",
    kind="kde",
    fill=True,
)
plt.show()
../../_images/02_seaborn_notebooks_3-34_distributions_displot_kde_univariado_6_0.png
[7]:
#
# Efecto de apilamiento.
#
sns.displot(
    penguins,
    x="flipper_length_mm",
    hue="species",
    kind="kde",
    multiple="stack",
)
plt.show()
../../_images/02_seaborn_notebooks_3-34_distributions_displot_kde_univariado_7_0.png
[8]:
#
# kde acumulada usando displot().
#
sns.displot(
    penguins,
    x="flipper_length_mm",
    kind="kde",
    color='red',
    fill=True,
    cumulative=True,
)
plt.show()
../../_images/02_seaborn_notebooks_3-34_distributions_displot_kde_univariado_8_0.png