The first factor we will be seeing is sex.
This graph shows the number of deaths and survivals based on gender.
import matplotlib.pyplot as plt
import seaborn as sns
# Group data by Sex and Survived and count the occurrences
sex_survival_counts = train_data.groupby(['Sex', 'Survived']).size().reset_index(name='Count')
# Map the Survived values to labels
sex_survival_counts['Survived'] = sex_survival_counts['Survived'].map({0: 'Deaths', 1: 'Survived'})
# Create a grouped bar plot
plt.figure(figsize=(8, 6))
sns.barplot(data=sex_survival_counts, x='Sex', y='Count', hue='Survived', palette='viridis')
plt.title('Survival and Death Counts by Sex')
plt.xlabel('Sex')
plt.ylabel('Number of Passengers')
plt.legend(title='Survival Status')
plt.show()
The second factor we will be seeing is SibSp, which means the number of siblings or spouses a passenger had on board.
import matplotlib.pyplot as plt
import seaborn as sns
# Create a count plot of the SibSp column
plt.figure(figsize=(7, 5))
sns.countplot(data=train_data, x='SibSp', palette='viridis')
plt.title('Count of Passengers by Number of Siblings/Spouses Aboard')
plt.xlabel('Number of Siblings/Spouses Aboard')
plt.ylabel('Number of Passengers')
plt.show()
The third factor we will be seeing is the embarkment point, which refers to the port from which passengers boarded the Titanic
import matplotlib.pyplot as plt
import seaborn as sns
# Create a count plot of the Embarked column
plt.figure(figsize=(7, 5))
sns.countplot(data=train_data, x='Embarked', palette='viridis')
plt.title('Passenger Count by Embarkation Port')
plt.xlabel('Embarkation Port')
plt.ylabel('Number of Passengers')
plt.show()
The third factor we will be seeing is class, which refers to the passenger’s travel class — First, Second, or Third.
import matplotlib.pyplot as plt
import seaborn as sns
# Create a count plot of the Pclass column
plt.figure(figsize=(7, 5))
sns.countplot(data=train_data, x='Pclass', palette='viridis')
plt.title('Passenger Count by Passenger Class')
plt.xlabel('Passenger Class')
plt.ylabel('Number of Passengers')
# Optional: Add labels for better readability
# plt.xticks([0, 1, 2], ['First Class', 'Second Class', 'Third Class'])
plt.show()