matplotlib探索泰坦尼克号灾难数据
时间: 2023-12-25 09:04:39 浏览: 78
以下是使用matplotlib探索泰坦尼克号灾难数据的示例:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 读取数据集
data = pd.read_csv('titanic.csv')
# 绘制生存率的柱状图
survived = data[data['Survived'] == 1]['Survived'].count()
not_survived = data[data['Survived'] == 0]['Survived'].count()
plt.bar(['Survived', 'Not Survived'], [survived, not_survived])
plt.xlabel('Survival')
plt.ylabel('Count')
plt.title('Survival Count')
plt.show()
# 绘制不同性别的生存率柱状图
male_survived = data[(data['Survived'] == 1) & (data['Sex'] == 'male')]['Survived'].count()
female_survived = data[(data['Survived'] == 1) & (data['Sex'] == 'female')]['Survived'].count()
male_not_survived = data[(data['Survived'] == 0) & (data['Sex'] == 'male')]['Survived'].count()
female_not_survived = data[(data['Survived'] == 0) & (data['Sex'] == 'female')]['Survived'].count()
plt.bar(['Male', 'Female'], [male_survived, female_survived], label='Survived')
plt.bar(['Male', 'Female'], [male_not_survived, female_not_survived], label='Not Survived', bottom=[male_survived, female_survived])
plt.xlabel('Sex')
plt.ylabel('Count')
plt.title('Survival Count by Sex')
plt.legend()
plt.show()
# 绘制不同年龄段的生存率柱状图
age_groups = pd.cut(data['Age'], bins=[0, 18, 30, 50, 100])
age_survived = data[data['Survived'] == 1].groupby(age_groups)['Survived'].count()
age_not_survived = data[data['Survived'] == 0].groupby(age_groups)['Survived'].count()
plt.bar(age_survived.index.astype(str), age_survived, label='Survived')
plt.bar(age_not_survived.index.astype(str), age_not_survived, label='Not Survived', bottom=age_survived)
plt.xlabel('Age Group')
plt.ylabel('Count')
plt.title('Survival Count by Age Group')
plt.legend()
plt.show()
```
阅读全文