首页 > 分享 > Python可视化seaborn练习题

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 sns1234567

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

鸢尾花萼片(sepal)和花瓣(petal)的大小关系(散点图)不同种类(species)鸢尾花萼片和花瓣的分布情况(箱图或者提琴图)鸢尾花萼片和花瓣大小的联合分布情况(六角箱图或者核密度估计)

data = sns.load_dataset("iris") data.head() # 萼片长度,萼片宽度,花瓣长度,花瓣宽度,种类123 sepal_lengthsepal_widthpetal_lengthpetal_widthspecies05.13.51.40.2setosa14.93.01.40.2setosa24.73.21.30.2setosa34.63.11.50.2setosa45.03.61.40.2setosa

data['sepal_size']=data['sepal_length']*data['sepal_width'] data['petal_size']=data['petal_length']*data['petal_width']12 萼片与花瓣

sns.lmplot(x='sepal_size',y='petal_size',data=data)1

<seaborn.axisgrid.FacetGrid at 0x7fea04b37550> 1

这里写图片描述

不同种类 萼片与花瓣分布

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

这里写图片描述

萼片与花瓣大小联合分布

# your code sns.jointplot(x='sepal_length',y='petal_length',data=data,kind='kde')12

/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> 12345678910

这里写图片描述

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

小费和总消费之间的关系(散点图+回归分析)男性顾客和女性顾客,谁更慷慨(箱图或者提琴图)抽烟与否是否会对小费金额产生影响(箱图或者提琴图)工作日和周末,什么时候顾客给的小费更慷慨(箱图或者提琴图)午饭和晚饭,哪一顿顾客更愿意给小费(箱图或者提琴图)就餐人数是否会对慷慨度产生影响(箱图或者提琴图)性别+抽烟的组合因素对慷慨度的影响(统计柱状图)

data = sns.load_dataset("tips") data.head() # 总消费,小费,性别,吸烟与否,就餐星期,就餐时间,就餐人数123 total_billtipsexsmokerdaytimesize016.991.01FemaleNoSunDinner2110.341.66MaleNoSunDinner3221.013.50MaleNoSunDinner3323.683.31MaleNoSunDinner2424.593.61FemaleNoSunDinner4 小费与总消费

sns.lmplot(x='total_bill',y='tip',data=data)1

<seaborn.axisgrid.FacetGrid at 0x7fe9ff3afed0> 1

这里写图片描述

小费:男性vs女性

sns.boxplot(y='tip',x='sex',data=data)1

<matplotlib.axes._subplots.AxesSubplot at 0x7fe9ff1651d0> 1

这里写图片描述

小费:抽烟vs不抽烟

sns.boxplot(y='tip',x='smoker',data=data)1

<matplotlib.axes._subplots.AxesSubplot at 0x7fe9fd4dac10> 1

这里写图片描述

小费:工作日vs周末

day=data['day'].unique() day12

[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()123 total_billtipsexsmokerdaytimesizeweek016.991.01FemaleNoSunDinner2weekend110.341.66MaleNoSunDinner3weekend221.013.50MaleNoSunDinner3weekend323.683.31MaleNoSunDinner2weekend424.593.61FemaleNoSunDinner4weekend

sns.boxplot(y='tip',x='week',data=data_expand)1

<matplotlib.axes._subplots.AxesSubplot at 0x7fe9ff533c10> 1

这里写图片描述

小费:午餐vs晚餐

sns.violinplot(x='time',y='tip',data=data)1

<matplotlib.axes._subplots.AxesSubplot at 0x7fe9ffcd35d0> 1

这里写图片描述

小费:就餐人数

sns.violinplot(x='size',y='tip',data=data)1

<matplotlib.axes._subplots.AxesSubplot at 0x7fe9ffef5650> 1

这里写图片描述

小费:性别+抽烟

# your code sns.barplot(x='sex',y='tip',hue='smoker',data=data)12

<matplotlib.axes._subplots.AxesSubplot at 0x7fe9ff4e3d10> 1

这里写图片描述

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

不同仓位等级中幸存和遇难乘客的分布(箱图或者提琴图)幸存和遇难乘客的票价分布(箱图或者提琴图)幸存和遇难乘客的年龄分布(箱图或者提琴图)不同上船港口的乘客仓位等级分布(箱图或者提琴图)幸存和遇难乘客堂兄弟姐妹的数量分布(箱图或者提琴图)幸存和遇难乘客父母子女的数量分布(箱图或者提琴图)单独乘船与否和幸存之间的关系(统计柱状图)乘客年龄和船票价格之间的关系(线性回归模型)乘客性别和仓位等级之间的关系(统计柱状图)乘客年龄和仓位等级之间的关系(带抖动的散点图)

data = sns.load_dataset("titanic") data.head() # 幸存与否,仓位等级,性别,年龄,堂兄弟姐妹数,父母子女数,票价,上船港口缩写,仓位等级,人员分类,是否成年男性,所在甲板,上船港口,是否幸存,是否单独乘船123 survivedpclasssexagesibspparchfareembarkedclasswhoadult_maledeckembark_townalivealone003male22.0107.2500SThirdmanTrueNaNSouthamptonnoFalse111female38.01071.2833CFirstwomanFalseCCherbourgyesFalse213female26.0007.9250SThirdwomanFalseNaNSouthamptonyesTrue311female35.01053.1000SFirstwomanFalseCSouthamptonyesFalse403male35.0008.0500SThirdmanTrueNaNSouthamptonnoTrue 幸存or遇难:不同仓位影响?

sns.violinplot(x='class',y='survived',data=data)1

<matplotlib.axes._subplots.AxesSubplot at 0x7fe9fcdcea50> 1

这里写图片描述

幸存or遇难:票价分布?

sns.violinplot(x='alive',y='fare',data=data)1

<matplotlib.axes._subplots.AxesSubplot at 0x7fe9fcd213d0> 1

这里写图片描述

幸存or遇难:年龄分布?

sns.violinplot(x='alive',y='age',data=data)1

<matplotlib.axes._subplots.AxesSubplot at 0x7fe9fcbd83d0> 1

这里写图片描述

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

sns.violinplot(x='embark_town',y='pclass',data=data)1

<matplotlib.axes._subplots.AxesSubplot at 0x7fe9fcaa7690> 1

这里写图片描述

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

sns.violinplot(x='alive',y='sibsp',data=data)1

<matplotlib.axes._subplots.AxesSubplot at 0x7fe9fc9f41d0> 1

这里写图片描述

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

sns.violinplot(x='alive',y='parch',data=data)1

<matplotlib.axes._subplots.AxesSubplot at 0x7fe9fc944d10> 1

这里写图片描述

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

# your code sns.barplot(x='alone',y='survived',data=data)12

<matplotlib.axes._subplots.AxesSubplot at 0x7fe9fc632ad0> 1

这里写图片描述

年龄与票价的关系

sns.lmplot(x='age',y='fare',data=data)1

<seaborn.axisgrid.FacetGrid at 0x7fe9fc5ba110> 1

这里写图片描述

性别与仓位等级

sns.barplot(x='sex',y='pclass',data=data)1

<matplotlib.axes._subplots.AxesSubplot at 0x7fe9fc508c10> 1

这里写图片描述

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

sns.lmplot(x='pclass',y='age',data=data,x_jitter=0.2)1

<seaborn.axisgrid.FacetGrid at 0x7fe9fc499150> 1

这里写图片描述

相关知识

如何用python代码做可视化 – PingCode
python 可视化包 Bokeh
基于Python实现交互式数据可视化的工具(用于Web)
如何利用Python的Seaborn和Matplotlib库对鸢尾花数据集进行数据可视化分析,并通过可视化结果分析不同鸢尾花种类的特征分布差异?
利用Python进行数据可视化常见的9种方法!超实用!
使用python对鸢尾花数据进行预处理和可视化
Python数据可视化中常用的绘图库有哪些? Python常用的绘图库
echart 三维可视化地图
在Python中用Seaborn美化图表的3个示例
Python编程之美:探索海棠花算法在数据可视化中的应用与实践

网址: Python可视化seaborn练习题 https://m.huajiangbk.com/newsview1946808.html

所属分类:花卉
上一篇: 花瓣地图最新版本app下载
下一篇: Python数据可视化 pyec