动手学数据分析 | 数据可视化(四)

下面代码为本篇内容的基本代码准备工作:

#!/usr/bin/env python
# coding: utf-8

get_ipython().run_line_magic('matplotlib', 'inline')
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

text = pd.read_csv('result.csv')
text.head()

目录

任务一:可视化展示泰坦尼克号数据集中男女中生存人数分布情况

任务二:可视化展示泰坦尼克号数据集中男女中生存与死亡人数的比例图

任务三:可视化展示泰坦尼克号数据集中不同票价的人生存和死亡人数分布情况

任务四:可视化展示泰坦尼克号数据集中不同仓位等级的人生存和死亡人员的分布情况

任务五:可视化展示泰坦尼克号数据集中不同年龄的人生存与死亡人数分布情况

任务六:可视化展示泰坦尼克号数据集中不同仓位等级的人年龄分布情况


任务一:可视化展示泰坦尼克号数据集中男女中生存人数分布情况

用柱状图展示:

sex = text.groupby('Sex')['Survived'].sum()
sex.plot.bar()
plt.title('survived_count')
plt.show()

输出结果:

任务二:可视化展示泰坦尼克号数据集中男女中生存与死亡人数的比例图

首先计算男女中死亡人数,1表示生存,0表示死亡:

text.groupby(['Sex', 'Survived'])['Survived'].count()
# Sex     Survived
# female  0            81
#         1           233
# male    0           468
#         1           109
# Name: Survived, dtype: int64

旋转行列:

text.groupby(['Sex', 'Survived'])['Survived'].count().unstack()
# Survived    0    1
# Sex               
# female     81  233
# male      468  109

stack()即“堆叠”,作用是将列旋转到行;unstack()即stack()的反操作,将行旋转到列。

用柱状图展示:

text.groupby(['Sex', 'Survived'])['Survived'].count().unstack().plot(kind='bar', stacked='True')
plt.title('survived_count')
plt.ylabel('count')

输出结果:

任务三:可视化展示泰坦尼克号数据集中不同票价的人生存和死亡人数分布情况

计算不同票价中生存与死亡人数 1表示生存,0表示死亡

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_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()

输出结果:

任务四:可视化展示泰坦尼克号数据集中不同仓位等级的人生存和死亡人员的分布情况

1表示生存,0表示死亡

pclass_sur = text.groupby(['Pclass'])['Survived'].value_counts()
pclass_sur
# Pclass  Survived
# 1       1           136
#         0            80
# 2       0            97
#         1            87
# 3       0           372
#         1           119
# Name: Survived, dtype: int64

import seaborn as sns
sns.countplot(x="Pclass", hue="Survived", data=text)

输出结果:

任务五:可视化展示泰坦尼克号数据集中不同年龄的人生存与死亡人数分布情况

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()

输出结果:

任务六:可视化展示泰坦尼克号数据集中不同仓位等级的人年龄分布情况

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")

输出结果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
泰坦尼克号数据_泰坦尼克号数据分析报告 891名乘客中遇难乘客有549⼈,占61.6%,⽣还乘客342⼈,占38.4%.各等级船舱乘客⼈数 各等级船舱乘客⼈数 Pclass_count=titanic_data['Pclass'].value_counts().sort_index() #⽤Bar_pie()函数作条形图和饼状图 Bar_pie(Pclass_count) 三等船舱乘客最多,占55.1%;⼀等船舱次之占24.2%;⼆级船舱乘客最少,占20.7%.男⼥乘客分布情况 男⼥乘客分布情况 Sex_count=titanic_data['Sex'].value_counts() print(Sex_count) Bar_pie(Sex_count) male 577 female 314 Name: Sex, dtype: int64 男乘客有577⼈,占64.8%;⼥乘客有314⼈,占35.2%.乘客年龄分布情况 乘客年龄分布情况 In [84]: #乘客年龄分布直⽅图 #创建figure、subplot,并⽤hist作条形图 fig_Age=plt.figure(figsize=(10,5)) ax_Age=fig_Age.add_subplot(1,2,1) titanic_data['Age'].hist(bins=10,color='g',alpha=0.3,grid=False) #设置x轴刻度标签 ax_Age.set_xticks([0,10,20,30,40,50,60,70,80,90,100]) #添加标题,x轴标签,y轴标签 ax_Age.set_title('Hist plot of Age') ax_Age.set_xlabel('Age') ax_Age.set_ylabel('number of people') #乘客年龄分布箱线图 #作箱线图 plt.subplot(122) titanic_data.boxplot(column='Age',showfliers=False) #添加y轴标签 plt.ylabel('Age') plt.title('boxplot of Fare') titanic_data['Age'].describe() count 891.000000 mean 29.544332 std 13.013778 min 0.000000 25% 22.000000 50% 29.000000 75% 35.000000 max 80.000000 Name: Age, dtype: float64 乘客年龄⼤概成正态分布,平均年龄29岁多,最⼤的80岁,最⼩的不到1岁(利⽤int()取整,不到1岁的为0).兄弟姐妹、配偶在船上的 兄弟姐妹、配偶在船上的 乘客分布情况条形图 乘客分布情况条形图 #创建figure、subplot,⽤plot()作柱状图 fig_SibSp=plt.figure(figsize=(10,5)) ax_SibSp=fig_SibSp.add_subplot(1,2,1) SibSp_count=titanic_data['SibSp'].value_counts() SibSp_count.plot(kind='bar') #添加标题,x轴标签,y轴标签 ax_SibSp.set_title('Bar plot of SibSp') ax_SibSp.set_xlabel('number of SibSp') ax_SibSp.set_ylabel('number of people') #拥有各 数量的兄弟姐妹、配偶的乘客⽐例条形图 plt.subplot(122) SibSp_count.div(SibSp_count.sum()).plot(kind='bar') #添加标题,x、y轴 标签 plt.title('Ratio of people in SibSp') plt.xlabel('SibSp') plt.ylabel('ratio') 在船上没有兄弟姐妹配偶的乘客较多,占68.2%.⽗母、孩⼦在船上的乘客分布条形图 ⽗母、孩⼦在船上的乘客分布条形图 Parch_count=titanic_data['Parch'].value_counts() #创建figure、subplot,⽤plot()作柱状图 fig_Parch=plt.figure(figsize=(10,5)) ax_Parch=fig_Parch.add_subplot(1,2,1) Parch_count.plot(kind='bar') #添加标题,x、y轴标签 ax_Parch.set_title('Bar plot of Parch') ax
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值