Matplotlib进阶版——Seaborn

在kaggle比赛做特征工程时接触到了seaborn库,去B站找了个视频入了门,整理了下知识点B站教学视频链接

整体风格布局设置

import seaborn as sns
sns.set()
#默认设置

五种主题风格:
darkgrid
whitegrid
dark
white
ticks

#用箱体图尝试5种风格
sns.set_style('whitegrid')
data = np.random.normal(size=(20,6))+np.arange(6)/2#这里用到了广播
sns.boxplot(data=data)

风格细节设置

sns.violinplot(data)
sns.despine()#默认去除右方和上方的轴框
sns.despine(offset=10)#offset——图距离轴线的距离
sns.despine(left=True)#隐藏某一个方向的轴

#with结构对某一子图进行系列设置
with sns.axes_style('darkgrid'):
    plt.subplot(211)
    sns.violinplot(data)

#图形大小设置,字体大小,线粗
sns.set_context('paper',font_scale=3,rc={'lines.linewidth':4.5})
plt.figure(figsize=(8,6))
sns.violinplot(data)

调色板

color_palette()能传入任何Matplotlib所支持的颜色
color_palette()不写参数则采用默认颜色
set_palette()设置所有图的颜色

单变量分析绘图

#直方图,柱形图
x=np.random.normal(size=10)
# sns.distplot(x,kde=False)#bins为系统默认分配
# sns.distplot(x,bins=20,kde=False)#指定bins
sns.distplot(x,kde=False,fit=stats.gamma)#在某一参量下拟合的轮廓

#根据均值和方差生成数据
mean,cov = [0,1],[(1,.5),(.5,1)]
data = np.random.multivariate_normal(mean,cov,200)
df = pd.DataFrame(data,columns=['x','y'])
print(df)

#观测两个变量之间的分布关系最好用散点图
sns.jointplot(x='x',y='y',data=df)
#会同时生成自身的分布以及两个变量之间的相关系数

#hex体现数据密集程度
x,y = np.random.multivariate_normal(mean,cov,1000)
with sns.axes_style('white'):
    sns.jointplot(x=x,y=y,kind='hex',color='k')

回归分析绘图

#加载内置数据集
iris = sns.load_dataset('iris')
sns.plot(iris)

对角线上是变量各自的直方分布图,其余位置是变量两两之间的散点图
在这里插入图片描述

sns.set(color_codes=True)
np.random.seed(sum(map(ord,"regression")))

tips = sns.load_dataset('tips')
tips.head()

#regplot和lmplot都可以绘制回归关系,推荐regplot
sns.regplot(x='total_bill',y='tip',data=tips,x_jitter=.05)#小范围浮动标志

多变量分析绘图

#stripplot
#多变量分析绘图
np.random.seed(sum(map(ord,'categorical')))
titanic = sns.load_dataset('titanic')
tips = sns.load_dataset('tips')
iris = sns.load_dataset('iris')

sns.stripplot(x='day',y='total_bill',data=tips)
#点太多时,会出现堆积现象

在这里插入图片描述

sns.stripplot(x='day',y='total_bill',data=tips,jitter=True)
#设为True时,一定程度上缓解堆积问题

在这里插入图片描述

sns.swarmplot(x='day',y='total_bill',hue='sex',data=tips)
#类似树形的图,‘sex’可以给点上颜色
#x,y坐标互换可以改变改变图形的横竖

在这里插入图片描述
在这里插入图片描述

盒图
IQR即统计学概念四分位距,第一四分位Q1与第三四分位Q3之间的距离
N=1.5IQR(通常,但也可根据数据改变)如果一个值>Q3+N或< Q1-N,则为离群点

#盒图,也是用来观测离群值的
sns.boxplot(x='day',y='total_bill',hue='time',data=tips)

箱体图上四条横线线分别代表最大值、四分之三位点、二分位点、四分之一位点、最小值。图上的零星点的分布代表离群点。
在这里插入图片描述

小提琴图

sns.violinplot(x='day',y='total_bill',hue='time',data=tips)

越胖的部位代表数据越多,一般都符合中间胖,两边瘦。
在这里插入图片描述
为了方便不同属性之间进行比较

sns.violinplot(x='day',y='total_bill',hue='sex',data=tips,split = True)#两个属性一半半,不再具有对称性

在这里插入图片描述

不同类型图之间的组合

    sns.violinplot(x='day',y='total_bill',data=tips,inner = None)
    sns.swarmplot(x='day',y='total_bill',data=tips,color = 'w',alpha = 0.9)#alpha的值和透明度相关

小提琴图和树状图的组合
在这里插入图片描述

分类属性绘图

条形图反应集中趋势,纵轴表示平均获救率。

sns.barplot(x = 'sex',y = 'survived',hue = 'class',data = titanic)

在这里插入图片描述
点图可以更好地表现变化差异

#点图
sns.pointplot(x = 'sex',y = 'survived',hue = 'class',data = titanic)
#点图格式设置
sns.pointplot(x = 'class',y = 'survived',hue = 'sex',data = titanic,
              palette = {"male":'g',"female":'m'},markers=["^","o"],linestyles=["-","--"])

在这里插入图片描述
在这里插入图片描述

多层面板分类图

sns.factorplot(x = 'day',y = 'total_bill',hue = 'smoker',data = tips)#m默认点图
sns.factorplot(x = 'day',y = 'total_bill',hue = 'smoker',data = tips,kind = 'bar')#kind=bar,为柱形图

在这里插入图片描述
在这里插入图片描述

sns.factorplot(x='day',y = 'total_bill',hue='smoker',col ='time',data=tips,kind='swarm')

在这里插入图片描述

sns.factorplot(x = 'time',y = 'total_bill',hue = 'smoker',col = 'day',data = tips,kind = 'box',size = 4,aspect = 5)#设置长宽比

在这里插入图片描述
Facetgrid使用方法

g = sns.FacetGrid(tips,col = 'time')#先实例化
g.map(plt.hist,"tip")

在这里插入图片描述

g = sns.FacetGrid(tips,col = 'sex',hue = 'smoker')
g.map(plt.scatter,"total_bill","tip",alpha = .7)#alpha越小越透明
g.add_legend()#图中添加图标

在这里插入图片描述
fit_reg = False

g = sns.FacetGrid(tips,row = 'smoker',col = 'time',margin_titles = True)
g.map(sns.regplot,'size','total_bill',color = '.1',fit_reg = True,x_jitter=.1)

在这里插入图片描述
fit_reg = True

g = sns.FacetGrid(tips,row = 'smoker',col = 'time',margin_titles = True)
g.map(sns.regplot,'size','total_bill',color = '.1',fit_reg = True,x_jitter=.1)#jitter:抖动

在这里插入图片描述
row_order自定义

from pandas import Categorical

ordered_days = tips.day.value_counts().index
print(ordered_days)
g = sns.FacetGrid(tips,row = 'day',row_order = ordered_days,size = 1.7,aspect = 4,)
g.map(sns.boxplot,'total_bill')

在这里插入图片描述

ordered_days = Categorical(['Thur','Fri','Sat','Sun'])
g = sns.FacetGrid(tips,row = 'day',row_order = ordered_days,size = 1.7,aspect = 4,)
g.map(sns.boxplot,'total_bill')

在这里插入图片描述
palette自定义

pal = dict(Lunch = 'seagreen',Dinner = 'gray')
g = sns.FacetGrid(tips , hue = 'time',palette=pal,size = 5)
g.map(plt.scatter , "total_bill",'tip',s = 50,alpha = .7,linewidth = .5,edgecolor = 'white')
g.add_legend()

在这里插入图片描述
marker自定义

g = sns.FacetGrid(tips,hue = "sex",palette='Set1',size = 5,hue_kws={"marker":["^","v"]})
g.map(plt.scatter,'total_bill','tip',s = 100,linewidth = 5,edgecolor = 'white')
g.add_legend()

在这里插入图片描述
图片布局与轴的设置

g = sns.FacetGrid(tips,row = 'sex',col = 'smoker',margin_titles = True,height = 2.5 )#先形成布局
g.map(plt.scatter,'total_bill','tip',color='blue',edgecolor = 'white',lw=.5)
g.set_axis_labels("Total bill(US Dollars)",'Tip')
g.set(xticks = [10,30,50],yticks = [2,6,10])
g.fig.subplots_adjust(wspace=.02,hspace=.02)

在这里插入图片描述

热度图

uniform_data = np.random.rand(3,3)
print(uniform_data)
heatmap = sns.heatmap(uniform_data)

把矩阵对应值的大小用颜色深浅表示出
在这里插入图片描述
数据不变,设置参数vmin,vmax

#控制取值范围
heatmap = sns.heatmap(uniform_data,vmin = 0.2,vmax=0.5)

在这里插入图片描述

normal_data = np.random.randn(3,3)
出现负值时的处理
heatmap = sns.heatmap(normal_data,center=0)

在这里插入图片描述
热度图反映数据大体变化趋势

flights = sns.load_dataset('flights')
flights = flights.pivot('month','year','passengers')
print(flights)
ax = sns.heatmap(flights)

在这里插入图片描述
显示数字

#让数字显示在热度图上
ax = sns.heatmap(flights,annot = True,fmt='d')

在这里插入图片描述
增加格线

ax = sns.heatmap(flights,linewidths=.5)

在这里插入图片描述
改变底色

#改变底色
ax = sns.heatmap(flights,cmap = 'YlGnBu')

在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值