Barplots usando catplot() —#

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

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

display(
    titanic.head(),
    titanic.tail(),
    titanic.size,
)
survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town alive alone
0 0 3 male 22.0 1 0 7.2500 S Third man True NaN Southampton no False
1 1 1 female 38.0 1 0 71.2833 C First woman False C Cherbourg yes False
2 1 3 female 26.0 0 0 7.9250 S Third woman False NaN Southampton yes True
3 1 1 female 35.0 1 0 53.1000 S First woman False C Southampton yes False
4 0 3 male 35.0 0 0 8.0500 S Third man True NaN Southampton no True
survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town alive alone
886 0 2 male 27.0 0 0 13.00 S Second man True NaN Southampton no True
887 1 1 female 19.0 0 0 30.00 S First woman False B Southampton yes True
888 0 3 female NaN 1 2 23.45 S Third woman False NaN Southampton no False
889 1 1 male 26.0 0 0 30.00 C First man True C Cherbourg yes True
890 0 3 male 32.0 0 0 7.75 Q Third man True NaN Queenstown no True
13365
[3]:
#
# Gráfico de barras con estimados de los
# intervalos de confianza por categoría usando
# bootstrap
#
sns.catplot(
    x="sex",
    y="survived",
    hue="class",
    kind="bar",
    data=titanic,
)

plt.show()
../../_images/02_seaborn_notebooks_4-43_categorical_bar_plots_3_0.png
[4]:
#
# Gráfico de barras horizontales
#
sns.catplot(
    x="survived",
    y="sex",
    hue="class",
    kind="bar",
    data=titanic,
)

plt.show()
../../_images/02_seaborn_notebooks_4-43_categorical_bar_plots_4_0.png
[5]:
#
# Modificación de la paleta de colores
sns.catplot(
    x="deck",
    kind="count",
    palette="ch:.25",
    data=titanic,
)

plt.show()
../../_images/02_seaborn_notebooks_4-43_categorical_bar_plots_5_0.png
[6]:
#
# Muchas de las opciones ya abordadas funcionan
# con estos gráficos
#
sns.catplot(
    y="deck",
    hue="class",
    kind="count",
    palette="pastel",
    edgecolor=".6",
    data=titanic,
)

plt.show()
../../_images/02_seaborn_notebooks_4-43_categorical_bar_plots_6_0.png