DW数据可视化


**复习: 在数据的清理和重构基础上,为了更好的看到每一个关键步骤的结果如何,使得数据更加的易于理解;*数据可视化是一个很有用的技巧。

柱状图

处理离散型数据

任务二:可视化展示泰坦尼克号数据集中男女中生存人数分布情况(用柱状图试试)。

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

在这里插入图片描述

任务三:可视化展示泰坦尼克号数据集中男女中生存人与死亡人数的比例图(用柱状图试试)。

# 提示:计算男女中死亡人数 1表示生存,0表示死亡
df=text.groupby(['Sex','Survived'])['Survived'].count().unstack()
print(df)
df.plot(kind='bar',stacked='True') # stacked=True: 表示将两个图标叠加在一起,默认False
plt.title('survived_count')
plt.ylabel('count')
Survived    0    1
Sex               
female     81  233
male      468  109

在这里插入图片描述

折线图

连续型数据
任务四:可视化展示泰坦尼克号数据集中不同票价的人生存和死亡人数分布情况。(用折线图试试)(横轴是不同票价,纵轴是存活人数)

# 计算不同票价中生存与死亡人数 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) #grid网格
plt.legend()
plt.show()

在这里插入图片描述
2.7.5 任务五:可视化展示泰坦尼克号数据集中不同仓位等级的人生存和死亡人员的分布情况。(用柱状图试试)

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)
<matplotlib.axes._subplots.AxesSubplot at 0x2752f2222b0>

在这里插入图片描述

kdeplot

2.7.6 任务六:可视化展示泰坦尼克号数据集中不同年龄的人生存与死亡人数分布情况。(不限表达方式)

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 0x2752c890e10>

在这里插入图片描述 任务七:可视化展示泰坦尼克号数据集中不同仓位等级的人年龄分布情况。(用折线图试试)

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 0x2752f19aa20>

在这里插入图片描述

散点图

import matplotlib.pyplot as plt
import numpy as np

n=1024
X=np.random.normal(0,1,n)#生成均值为0,方差是1的n个随机数
Y=np.random.normal(0,1,n)
T=np.arctan2(Y,X)#for color value

plt.scatter(X,Y,s=75,c=T,alpha=0.5)

plt.xlim((-1.5,1.5))
plt.ylim((-1.5,1.5))

plt.xticks(())
plt.yticks(())   #隐藏所有的横纵坐标值

plt.show()

在这里插入图片描述

3D图

from mpl_toolkits.mplot3d import Axes3D

#构建3D坐标网格
fig=plt.figure()
ax=Axes3D(fig)

X=np.arange(-4,4,0.25)
Y=np.arange(-4,4,0.25)
X,Y=np.meshgrid(X,Y)
R=np.sqrt(X**2+Y**2)
Z=np.sin(R)

#画3d图
ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow')) #h黑色的线的跨度 行跨rstride和列跨cstride
#3d图的投影 投影方向zdir为z轴,offset表示投到z=-2这个平面上
ax.contourf(X,Y,Z,zdir='z',offset=-2,cmap='rainbow')

ax.set_zlim(-2,2)
plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值