数据可视化
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#导入result.csv这个文件
text=pd.read_csv(r'result.csv')
1.柱状图展示数据集中男女生存人数分布情况
sex = text.groupby('Sex')['Survived'].sum() sex.plot.bar()
plt.title('survived_count')
plt.show()
2.柱状图展示生存人与死亡人数的比例
# 提示:计算男女中死亡人数 1表示生存,0表示死亡
text.groupby(['Sex','Survived']) ['Survived'].count().unstack().plot(kind='bar',stacked='True') plt.title('survived_count')
plt.ylabel('count')
Text(0, 0.5, 'count')
3.折线图展示不同票价的人生存和死亡人数分布情况
# 计算不同票价中生存与死亡人数 1表示生存,0表示死亡
fare_sur = text.groupby(['Fare'])['Survived'].value_counts().sort_values(ascending=False) fare_sur
#排序后绘折线图
fig = plt.figure(figsize=(20, 18))
fare_sur.plot(grid=True)
plt.legend()
plt.show()
# 排序前绘折线图
fare_sur1= text.groupby(['Fare'])['Survived'].value_counts()
fare_sur1
fig = plt.figure(figsize=(20, 18)) fare_sur1.plot(grid=True)
plt.legend()
plt.show()
4.柱状图不同仓位等级的人生存和死亡人员的分布情况
# 1表示生存,0表示死亡
pclass_sur = text.groupby(['Pclass'])['Survived'].value_counts()
pclass_sur
import seaborn as sns
sns.countplot(x="Pclass", hue="Survived", data=text)
5.展示不同年龄的人生存与死亡人数分布情况
facet = sns.FacetGrid(text, hue="Survived",aspect=3) facet.map(sns.kdeplot,'Age',shade= True)
facet.set(xlim=(0, text['Age'].max()))
facet.add_legend()
6.展示不同仓位等级的人年龄分布情况
text.Age[text.Pclass == 1].plot(kind='kde')
text.Age[text.Pclass == 2].plot(kind='kde')
text.Age[text.Pclass == 3].plot(kind='kde')
plt.xlabel("age")
plt.legend((1,2,3),loc="best")