Task04:数据可视化
- 本文参考datawhale开源学习资料
- 本文学习中参考该视频
开始之前,导入numpy、pandas包和数据
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline的作用:
- 调用matplotlib.pyplot的绘图函数plot()进行绘图的时候,或者生成一个figure画布的时候,可以直接在python console里面生成图像;
- 如果不加这一句的话,我们在画图结束之后需要加上plt.show()才可以显示图像。
导入result.csv这个文件
text = pd.read_csv(r'result.csv')
text.head(2)
Unnamed: 0 | PassengerId | Survived | Pclass | Name | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S |
1 | 1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C |
2.7 如何让人一眼看懂你的数据?
2.7.1 任务一:跟着书本第九章,了解matplotlib,自己创建一个数据项,对其进行基本可视化
【思考】最基本的可视化图案有哪些?分别适用于那些场景?
相关可以参考Matplotlib画各种图。
2.7.2 任务二:用柱状图可视化展示泰坦尼克号数据集中男女中生存人数分布情况。
# 使用groupby函数统计男女总人数
sex = text.groupby('Sex')['Survived'].sum()
print(sex)
# 画柱状图
sex.plot.bar()
# 标题名
plt.title('survived_count')
# 因为前面使用了%matplotlib inline,plt.show()可加可不加
plt.show()
Sex
female 233
male 109
Name: Survived, dtype: int64
【思考】计算出泰坦尼克号数据集中男女中死亡人数,并可视化展示?如何和男女生存人数可视化柱状图结合到一起?看到你的数据可视化,说说你的第一感受(比如:你一眼看出男生存活人数更多,那么性别可能会影响存活率)。
sex_survived = text.groupby(['Sex','Survived'] )['Survived'].count().unstack()
sex_survived
Survived | 0 | 1 |
---|---|---|
Sex | ||
female | 81 | 233 |
male | 468 | 109 |
died = sex_survived[0]
died
Sex
female 81
male 468
Name: 0, dtype: int64
# 画柱状图
died.plot.bar()
# 标题名
plt.title('died_count')
Text(0.5, 1.0, 'died_count')
- 可以观察到男性死亡人数远大于女性。
2.7.3 任务三:用柱状图可视化展示泰坦尼克号数据集中男女中生存人与死亡人数的比例图。
text.groupby(['Sex','Survived'])['Survived'].count().unstack().plot(kind='bar',stacked='True')
plt.title('survived_count')
plt.ylabel('count')
Text(0, 0.5, 'count')
2.7.4 任务四:用折线图可视化展示泰坦尼克号数据集中不同票价的人生存和死亡人数分布情况。(横轴是不同票价,纵轴是存活人数)
【提示】对于这种统计性质的且用折线表示的数据,你可以考虑将数据排序或者不排序来分别表示。看看你能发现什么?
# 计算不同票价中生存与死亡人数 1表示生存,0表示死亡
fare_sur = text.groupby(['Fare'])['Survived'].value_counts().sort_values(ascending=False)
fare_sur
Fare Survived
8.0500 0 38
7.8958 0 37
13.0000 0 26
7.7500 0 22
26.0000 0 16
..
20.2500 1 1
0 1
18.7875 1 1
0 1
15.0500 0 1
Name: Survived, Length: 330, dtype: int64
# 排序后绘折线图
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
Fare Survived
0.0000 0 14
1 1
4.0125 0 1
5.0000 0 1
6.2375 0 1
..
247.5208 1 1
262.3750 1 2
263.0000 0 2
1 2
512.3292 1 3
Name: Survived, Length: 330, dtype: int64
fig = plt.figure(figsize=(20, 18))
fare_sur1.plot(grid=True)
plt.legend()
plt.show()
fare = text.groupby(['Fare','Survived'])['Survived'].count().unstack()
fare.plot()
<AxesSubplot:xlabel='Fare'>
- 可以观察到低票价的死亡人数多,死亡概率大
2.7.5 任务五:用柱状图可视化展示泰坦尼克号数据集中不同仓位等级的人生存和死亡人员的分布情况。
# 1表示生存,0表示死亡
pclass_sur = text.groupby(['Pclass','Survived'])['Survived'].count().unstack()
pclass_sur
Survived | 0 | 1 |
---|---|---|
Pclass | ||
1 | 80 | 136 |
2 | 97 | 87 |
3 | 372 | 119 |
pclass_sur.plot.bar()
<AxesSubplot:xlabel='Pclass'>
# 另外一个数据可视化包
import seaborn as sns
sns.countplot(x="Pclass", hue="Survived", data=text)
<AxesSubplot:xlabel='Pclass', ylabel='count'>
- 可以观察到头等舱的存活概率大,三等舱的死亡概率大
【思考】看到这个前面几个数据可视化,说说你的第一感受和你的总结
- 男性死亡人数远大于女性;
- 低票价的死亡人数多,死亡概率大;
- 舱位等级越高,存活率越高。
2.7.6 任务六:可视化展示泰坦尼克号数据集中不同年龄的人生存与死亡人数分布情况。
- 将年龄分成5段,绘制直方图
# 死亡人数
text.Age[text.Survived == 0].hist(bins=5, alpha=0.4)
# 生存人数
text.Age[text.Survived == 1].hist(bins=5, alpha=0.4)
# 加入图注
plt.legend((0,1))
# 横坐标标签
plt.xlabel('age')
# 纵坐标标签
plt.ylabel('count')
Text(0, 0.5, 'count')
- 密度曲线
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()
<seaborn.axisgrid.FacetGrid at 0x1462bf005c0>
2.7.7 任务七:用折线图可视化展示泰坦尼克号数据集中不同仓位等级的人年龄分布情况。
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")
<matplotlib.legend.Legend at 0x1462c506390>
# 使用seaborn
unique_pclass = text.Pclass.unique()
unique_pclass.sort()
unique_pclass
array([1, 2, 3], dtype=int64)
for i in unique_pclass:
sns.kdeplot(text.Age[text.Pclass == i],shade=True,linewidth=0)
plt.xlabel("age")
plt.legend(unique_pclass)
<matplotlib.legend.Legend at 0x1462bf11a20>
【思考】上面所有可视化的例子做一个总体的分析,你看看你能不能有自己发现
1.年纪大死亡率较高;
2.头等舱人员(平均)年龄 > 二等舱 > 三等舱。