Python可视化seaborn练习题

seaborn —— 课后练✋

%matplotlib inline
import numpy as np
import pandas as pd
from scipy import stats, integrate
import matplotlib as mpl
from matplotlib import pyplot as plt
import seaborn as sns

练习1:鸢尾花花型尺寸分析

  • 鸢尾花萼片(sepal)和花瓣(petal)的大小关系(散点图)
  • 不同种类(species)鸢尾花萼片和花瓣的分布情况(箱图或者提琴图)
  • 鸢尾花萼片和花瓣大小的联合分布情况(六角箱图或者核密度估计)
data = sns.load_dataset("iris")
data.head()
# 萼片长度,萼片宽度,花瓣长度,花瓣宽度,种类
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa
data['sepal_size']=data['sepal_length']*data['sepal_width']
data['petal_size']=data['petal_length']*data['petal_width']

萼片与花瓣

sns.lmplot(x='sepal_size',y='petal_size',data=data)
<seaborn.axisgrid.FacetGrid at 0x7fea04b37550>

这里写图片描述

不同种类 萼片与花瓣分布

g = sns.PairGrid(data,
                 x_vars=["species"],
                 y_vars=["sepal_size", "petal_size"],
                 aspect=2, size=4)
g.map(sns.violinplot, palette="pastel");

这里写图片描述

萼片与花瓣大小联合分布

# your code
sns.jointplot(x='sepal_length',y='petal_length',data=data,kind='kde')
/opt/ds/local/lib/python2.7/site-packages/numpy/ma/core.py:6385: MaskedArrayFutureWarning: In the future the default for ma.minimum.reduce will be axis=0, not the current None, to match np.minimum.reduce. Explicitly pass 0 or None to silence this warning.
  return self.reduce(a)
/opt/ds/local/lib/python2.7/site-packages/numpy/ma/core.py:6385: MaskedArrayFutureWarning: In the future the default for ma.maximum.reduce will be axis=0, not the current None, to match np.maximum.reduce. Explicitly pass 0 or None to silence this warning.
  return self.reduce(a)





<seaborn.axisgrid.JointGrid at 0x7fe9fc6fd250>

这里写图片描述

练习2:餐厅小费情况分析

  • 小费和总消费之间的关系(散点图+回归分析)
  • 男性顾客和女性顾客,谁更慷慨(箱图或者提琴图)
  • 抽烟与否是否会对小费金额产生影响(箱图或者提琴图)
  • 工作日和周末,什么时候顾客给的小费更慷慨(箱图或者提琴图)
  • 午饭和晚饭,哪一顿顾客更愿意给小费(箱图或者提琴图)
  • 就餐人数是否会对慷慨度产生影响(箱图或者提琴图)
  • 性别+抽烟的组合因素对慷慨度的影响(统计柱状图)
data = sns.load_dataset("tips")
data.head()
# 总消费,小费,性别,吸烟与否,就餐星期,就餐时间,就餐人数
total_billtipsexsmokerdaytimesize
016.991.01FemaleNoSunDinner2
110.341.66MaleNoSunDinner3
221.013.50MaleNoSunDinner3
323.683.31MaleNoSunDinner2
424.593.61FemaleNoSunDinner4

小费与总消费

sns.lmplot(x='total_bill',y='tip',data=data)
<seaborn.axisgrid.FacetGrid at 0x7fe9ff3afed0>

这里写图片描述

小费:男性vs女性

sns.boxplot(y='tip',x='sex',data=data)
<matplotlib.axes._subplots.AxesSubplot at 0x7fe9ff1651d0>

这里写图片描述

小费:抽烟vs不抽烟

sns.boxplot(y='tip',x='smoker',data=data)
<matplotlib.axes._subplots.AxesSubplot at 0x7fe9fd4dac10>

这里写图片描述

小费:工作日vs周末

day=data['day'].unique()
day
[Sun, Sat, Thur, Fri] Categories (4, object): [Sun, Sat, Thur, Fri]
data_week=pd.DataFrame(('weekend' if x in ['Sun','Sat'] else 'weekday' for x in data.day),index=data.index,columns=['week'])
data_expand=pd.merge(data,data_week,left_index=True,right_index=True)
data_expand.head()
total_billtipsexsmokerdaytimesizeweek
016.991.01FemaleNoSunDinner2weekend
110.341.66MaleNoSunDinner3weekend
221.013.50MaleNoSunDinner3weekend
323.683.31MaleNoSunDinner2weekend
424.593.61FemaleNoSunDinner4weekend
sns.boxplot(y='tip',x='week',data=data_expand)
<matplotlib.axes._subplots.AxesSubplot at 0x7fe9ff533c10>

这里写图片描述

小费:午餐vs晚餐

sns.violinplot(x='time',y='tip',data=data)
<matplotlib.axes._subplots.AxesSubplot at 0x7fe9ffcd35d0>

这里写图片描述

小费:就餐人数

sns.violinplot(x='size',y='tip',data=data)
<matplotlib.axes._subplots.AxesSubplot at 0x7fe9ffef5650>

这里写图片描述

小费:性别+抽烟

# your code
sns.barplot(x='sex',y='tip',hue='smoker',data=data)
<matplotlib.axes._subplots.AxesSubplot at 0x7fe9ff4e3d10>

这里写图片描述

练习3:泰坦尼克号海难幸存状况分析

  • 不同仓位等级中幸存和遇难乘客的分布(箱图或者提琴图)
  • 幸存和遇难乘客的票价分布(箱图或者提琴图)
  • 幸存和遇难乘客的年龄分布(箱图或者提琴图)
  • 不同上船港口的乘客仓位等级分布(箱图或者提琴图)
  • 幸存和遇难乘客堂兄弟姐妹的数量分布(箱图或者提琴图)
  • 幸存和遇难乘客父母子女的数量分布(箱图或者提琴图)
  • 单独乘船与否和幸存之间的关系(统计柱状图)
  • 乘客年龄和船票价格之间的关系(线性回归模型)
  • 乘客性别和仓位等级之间的关系(统计柱状图)
  • 乘客年龄和仓位等级之间的关系(带抖动的散点图)
data = sns.load_dataset("titanic")
data.head()
# 幸存与否,仓位等级,性别,年龄,堂兄弟姐妹数,父母子女数,票价,上船港口缩写,仓位等级,人员分类,是否成年男性,所在甲板,上船港口,是否幸存,是否单独乘船
survivedpclasssexagesibspparchfareembarkedclasswhoadult_maledeckembark_townalivealone
003male22.0107.2500SThirdmanTrueNaNSouthamptonnoFalse
111female38.01071.2833CFirstwomanFalseCCherbourgyesFalse
213female26.0007.9250SThirdwomanFalseNaNSouthamptonyesTrue
311female35.01053.1000SFirstwomanFalseCSouthamptonyesFalse
403male35.0008.0500SThirdmanTrueNaNSouthamptonnoTrue

幸存or遇难:不同仓位影响?

sns.violinplot(x='class',y='survived',data=data)
<matplotlib.axes._subplots.AxesSubplot at 0x7fe9fcdcea50>

这里写图片描述

幸存or遇难:票价分布?

sns.violinplot(x='alive',y='fare',data=data)
<matplotlib.axes._subplots.AxesSubplot at 0x7fe9fcd213d0>

这里写图片描述

幸存or遇难:年龄分布?

sns.violinplot(x='alive',y='age',data=data)
<matplotlib.axes._subplots.AxesSubplot at 0x7fe9fcbd83d0>

这里写图片描述

不同上船港口的仓位等级分布

sns.violinplot(x='embark_town',y='pclass',data=data)
<matplotlib.axes._subplots.AxesSubplot at 0x7fe9fcaa7690>

这里写图片描述

幸存or遇难:堂兄弟姐妹数量分布?

sns.violinplot(x='alive',y='sibsp',data=data)
<matplotlib.axes._subplots.AxesSubplot at 0x7fe9fc9f41d0>

这里写图片描述

幸存or遇难:父母子女数量分布?

sns.violinplot(x='alive',y='parch',data=data)
<matplotlib.axes._subplots.AxesSubplot at 0x7fe9fc944d10>

这里写图片描述

幸存or遇难:是否单独乘船?

# your code
sns.barplot(x='alone',y='survived',data=data)
<matplotlib.axes._subplots.AxesSubplot at 0x7fe9fc632ad0>

这里写图片描述

年龄与票价的关系

sns.lmplot(x='age',y='fare',data=data)
<seaborn.axisgrid.FacetGrid at 0x7fe9fc5ba110>

这里写图片描述

性别与仓位等级

sns.barplot(x='sex',y='pclass',data=data)
<matplotlib.axes._subplots.AxesSubplot at 0x7fe9fc508c10>

这里写图片描述

乘客年龄与仓位等级的关系

sns.lmplot(x='pclass',y='age',data=data,x_jitter=0.2)
<seaborn.axisgrid.FacetGrid at 0x7fe9fc499150>

这里写图片描述

  • 6
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值