Tutorial for Seaborn: Exploring Titanic Survival Data

In this tutorial for Seaborn, we’ll walk through how to use Seaborn, a statistical data visualization library in Python, to explore the famous Titanic dataset. We’ll use various visualizations to understand factors that contributed to survival.

Table of Contents

  1. Setup and Installation
  2. Reading Data
  3. Basic Plots
  4. Categorical Data Plots
  5. Distribution Plots
  6. Pair Plots
  7. Heatmaps
  8. Conclusion

1. Setup and Installation

Install Seaborn and Pandas:

pip install seaborn pandas

2. Reading Data

Seaborn comes with some built-in datasets, one of which is the Titanic dataset.

Python
import seaborn as sns
import pandas as pd

# Load the Titanic dataset
df = sns.load_dataset('titanic')

3. Basic Plots

3.1 Count Plot

Visualize the number of survivors and non-survivors.

Python
sns.countplot(x='survived', data=df)
Basic Plot

4. Categorical Data Plots

4.1 Survival by Class

Use a bar plot to show survival rates by class.

Python
sns.barplot(x='class', y='survived', data=df)
Categorical Data Plots

4.2 Survival by Sex and Class

Show survival by both sex and class.

Python
sns.catplot(x='class', y='survived', hue='sex', kind='bar', data=df)
Categorical Data Plots

5. Distribution Plots

5.1 Age Distribution of Survivors

Plot a histogram to show age distribution among survivors.

Python
sns.histplot(df[df['survived'] == 1]['age'], kde=True)
Distribution Plots

6. Pair Plots

Plot pairwise relationships in the dataset.

Python
sns.pairplot(df[['age', 'fare', 'class', 'survived']], hue='survived')
pair Plot

7. Heatmaps

Visualize a correlation matrix to explore relationships between numerical features.

Python
# Compute correlation matrix
corr_matrix = df.corr()

# Draw a heatmap
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm')
Heatmap

8. Conclusion

We used Seaborn to explore the Titanic dataset, visualizing survival rates by different factors like class and sex, distributions of numerical variables, and correlations between features.

Congratulations, you’ve successfully completed the tutorial! The Seaborn library offers many more types of plots and customization options to explore.

Feel free to adapt these methods to your own datasets and research questions!