Load Titanic data from sklearn library, plot the following with proper legend and axis labels: a. Plot bar chart to show the frequency of survivors and non-survivors for male and female passengers separately b. Draw a scatter plot for any two selected features c. Compare density distribution for features age and passenger fare d. Use a pair plot to show pairwise bivariate distribution
Load Titanic data from sklearn library, plot the following with proper legend and axis labels:
a. Plot bar chart to show the frequency of survivors and non-survivors for male and
female passengers separately
b. Draw a scatter plot for any two selected features
c. Compare density distribution for features age and passenger fare
d. Use a pair plot to show pairwise bivariate distribution
import seaborn as sns
import matplotlib.pyplot as plt
# Load the Titanic dataset from seaborn
titanic = sns.load_dataset('titanic')
# Plot bar chart to show the frequency of survivors and non-survivors for male and female passengers separately
plt.figure(figsize=(8, 6))
sns.countplot(x='sex', hue='survived', data=titanic)
plt.xlabel('Sex')
plt.ylabel('Frequency')
plt.title('Survivors vs Non-Survivors by Gender')
plt.legend(title='Survived', labels=['No', 'Yes'])
plt.show()
# Draw a scatter plot for any two selected features
plt.figure(figsize=(8, 6))
sns.scatterplot(x='age', y='fare', data=titanic)
plt.xlabel('Age')
plt.ylabel('Fare')
plt.title('Scatter Plot: Age vs Fare')
plt.show()
# Compare density distribution for features age and passenger fare
plt.figure(figsize=(8, 6))
sns.kdeplot(data=titanic, x='age', label='Age')
sns.kdeplot(data=titanic, x='fare', label='Fare')
plt.xlabel('Value')
plt.ylabel('Density')
plt.title('Density Distribution: Age vs Fare')
plt.legend()
plt.show()
# Use a pair plot to show pairwise bivariate distribution
sns.pairplot(titanic)
plt.show()
Comments
Post a Comment