回归分析、多变量分析绘图、#分类属性绘图、多层面板分类绘图

本文详细介绍使用Seaborn库进行数据可视化的方法,包括回归分析、多变量分析、分类属性绘图等,通过实例展示了如何利用各种图表如条形图、点图、小提琴图等进行数据探索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib as mpl
iris=sns.load_dataset("iris")
sns.pairplot(iris)#pairplot多变量图
<seaborn.axisgrid.PairGrid at 0x20463b66208>

在这里插入图片描述

##回归分析##
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib as mpl
np.random.seed(sum(map(ord,"regression")))
sns.set(color_codes=True)
tips=sns.load_dataset("tips")
tips.head()
total_billtipsexsmokerdaytimesize
016.991.01FemaleNoSunDinner2
110.341.66MaleNoSunDinner3
221.013.50MaleNoSunDinner3
323.683.31MaleNoSunDinner2
424.593.61FemaleNoSunDinner4
sns.regplot(x="total_bill",y="tip",data=tips)#用regplot做回归分析
<matplotlib.axes._subplots.AxesSubplot at 0x2046459de10>

在这里插入图片描述

sns.lmplot(x="total_bill",y="tip",data=tips)#用regplot做回归分析
<seaborn.axisgrid.FacetGrid at 0x20464a80828>

在这里插入图片描述

sns.regplot(x="size",y="tip",data=tips,x_jitter=0.05)#在原始点上加上小范围的浮动
<matplotlib.axes._subplots.AxesSubplot at 0x204649a4668>

在这里插入图片描述

##多变量分析绘图(带有类别属性的变量)##
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib as mpl
np.random.seed(sum(map(ord,"regression")))
sns.set(color_codes=True)
tips=sns.load_dataset("tips")
tips.head(5)
total_billtipsexsmokerdaytimesize
016.991.01FemaleNoSunDinner2
110.341.66MaleNoSunDinner3
221.013.50MaleNoSunDinner3
323.683.31MaleNoSunDinner2
424.593.61FemaleNoSunDinner4
sns.stripplot(x="day",y="tip",data=tips,jitter=True)#jitter=True使散点发生偏移,看的更清楚
<matplotlib.axes._subplots.AxesSubplot at 0x20464cee4e0>

在这里插入图片描述)

sns.swarmplot(x="day",y="total_bill",hue="sex",data=tips)#散点左右分布更加均匀 hue="sex"加入不同性别比较
<matplotlib.axes._subplots.AxesSubplot at 0x20464d69550>

在这里插入图片描述

sns.boxplot(x="day",y="total_bill",hue="time",data=tips)#盒图
<matplotlib.axes._subplots.AxesSubplot at 0x20464da14a8>

在这里插入图片描述

sns.violinplot(x="total_bill",y="day",hue="time",data=tips)#小提琴图 split=True
<matplotlib.axes._subplots.AxesSubplot at 0x20464e07860>

在这里插入图片描述

sns.violinplot(y="total_bill",x="day",hue="sex",split=True,data=tips)#split=True 显示的时候hue在小提琴左右显示
<matplotlib.axes._subplots.AxesSubplot at 0x2046606ada0>

在这里插入图片描述

#分类属性绘图
sns.swarmplot(x="day",y="tip",data=tips)
sns.violinplot(x="day",y="tip",data=tips,color="w",alpha=.5,inner=None)#alpha设置透明度
<matplotlib.axes._subplots.AxesSubplot at 0x204662d4860>

在这里插入图片描述

titanic=sns.load_dataset("titanic")
titanic.head()
survivedpclasssexagesibspparchfareembarkedclasswhoadult_maledeckembark_townalivealone
003male22.0107.2500SThirdmanTrueNaNSouthamptonnoFalse
111female38.01071.2833CFirstwomanFalseCCherbourgyesFalse
213female26.0007.9250SThirdwomanFalseNaNSouthamptonyesTrue
311female35.01053.1000SFirstwomanFalseCSouthamptonyesFalse
403male35.0008.0500SThirdmanTrueNaNSouthamptonnoTrue
sns.barplot(x="sex",y="survived",hue="class",data=titanic)#条形图
<matplotlib.axes._subplots.AxesSubplot at 0x20466414a90>

在这里插入图片描述

sns.pointplot(x="sex",y="survived",hue="class",data=titanic)#点图
<matplotlib.axes._subplots.AxesSubplot at 0x2046640aba8>

在这里插入图片描述

sns.pointplot(x="class",y="survived",hue="sex",data=titanic,
             palette={"male":"r","female":"m"},
             markers=["^","o"],linestyles=["-","--"])
<matplotlib.axes._subplots.AxesSubplot at 0x204667812e8>

在这里插入图片描述

#多层面板分类图
sns.factorplot(x="day",y="total_bill",hue="smoker",data=tips)
<seaborn.axisgrid.FacetGrid at 0x204667eb9e8>

在这里插入图片描述

sns.factorplot(x="day",y="total_bill",hue="smoker",data=tips,col="time",kind="swarm")#加入time维度,kind将swarm整合进去
<seaborn.axisgrid.FacetGrid at 0x204666fbe48>

在这里插入图片描述

sns.factorplot(x="time",y="total_bill",hue="smoker",data=tips,col="day",kind="box",size=10,aspect=0.5)#aspect长宽比
<seaborn.axisgrid.FacetGrid at 0x20469ba0cf8>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值