seaborn 是在matplotlib的基础上进行了更高级的API封装,从而使得作图更加容易,在大多数情况下使用seaborn就能做出很具有吸引力的图。(也就是说plt和sns中会有很多方法是可以混用的)本博文中的数据集采用Kaggle中的compete:House Prices: Advanced Regression Techniques中train.csv。如果需要数据集在Kaggle中就可以下载。本博文是为了方便博主自己查询seaborn中常用的技巧,仅记录常用的内容。伪系统性学习或者在做案例时遇见的内容我会记录在这里,不定期更新。
0.引用模块
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
train_data = pd.read_csv('C:/ml_data/house/naive/train.csv')
1.seaborn以礼始以礼终
1.1 sns设备背景颜色风格
sns.set_style('white')
可选背景 white,dark,writegrid,darkgrid,ticks。使用white就挺舒适的,并不建议更换。
1.2 新建画布
plt.figure()
方法入参及含义
num:图像编号或名称,数字为编号 ,字符串为名称
figsize:指定figure的宽和高,单位为英寸;(1英寸等于2.5cm,A4纸是 21*30cm的纸张 )
dpi参数指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80
facecolor:背景颜色
edgecolor:边框颜色
frameon:是否显示边框
1.3保存图片
plt.savefig('c:/ml_data/titanic.png')
注意:
1.需首先调用plt.figure()
2.如果在plt.savefig之前调用plt.show(),会保存一片空白!因为在调用plt.show()时会自动新建一个画布
2.绘制直方图 single
适用于单个特征,看该特征的分布
sns.distplot
(
data=train_data[‘salesprice’]
hist = False / True #是否绘制柱状图
kde = False / True #是否绘制密度估计曲线,默认为True
bins = #将原始数据划分到n个桶中(按照数值划分)
rug = False / True 是否绘制边际毛毯
ax = axes[0] #放在subplot中哪个位置 axes = plt.subplots(2,1)
ax = axes[1,0]
hist_kws = {‘color’:‘red’,‘label’:‘green’} #给柱上颜色和标记
kde_kws = {‘color’:‘red’,‘label’:‘green’,‘linestyle’:’–’} #给kde上颜色和标记,设置风格
fit = norm 查看该特诊数值所匹配的正态分布(看偏差)
)
plt.figure()
sns.distplot(train_data['SalePrice'])
plt.savefig('C:/Users/lifeisshort/Desktop.temp.jpg')
3.计数图 single
单个特征,每个特征可取值中的数量
sns.countplot
(
数据风格不再赘述
x = ‘Age’
hue = ‘Sex’
data = train_data
)
plt.figure()
sns.countplot(x = 'MSSubClass',hue = 'Street',data = train_data)
plt.savefig('C:/Users/lifeisshort/Desktop/temp.jpg')
4.绘制条形图 multiple
显示y特征在x特征的作用下的分布情况,适用于x特征为类别特征,数值型特征别找死。
sns.barplot
(
数据风格1:
x = train_data['Age] #横轴
y = train_data[‘Survived’] #纵轴
hue = train_data[‘Sex’] #横轴的数据再细分一下下
数据风格2:
x = ‘Age’
y = ‘Survived’
hue = ‘Sex’
data = train_data
estimator = np.mean / np.median / np.max /np.min 默认是均值
ci=0,'sd’等,统计学中的置信区间
)
plt.figure()
sns.barplot(y = 'SalePrice',x = 'MSSubClass',data = train_data,estimator= np.max)
plt.savefig('C:/Users/lifeisshort/Desktop.temp.jpg')
可以参考这个链接,全一点:https://blog.csdn.net/zyb228/article/details/101938152
5.散点图 multiple
在x特征的作用下y特征的分布情况 适用于类别特征,数值型特征别找死
与sns.barplot不同的是,sns.barplot重点于数据的某个特征值的比较,而该方法重点于所有数据点的分布
1.使用sns.stripplot 应用于大数据
sns.stripplot
(
x = ‘Age’
y = ‘fare’
hue = ‘Sex’
data = train_data
jitter = n #同类数据的晃动(最大间距)
)
plt.figure()
sns.stripplot(x = 'MSSubClass',y = 'SalePrice',data = train_data)
plt.savefig('C:/Users/lifeisshort/Desktop/temp.jpg')
2.使用swarmplot 应用于小数据 更好看,前提是数据量不多
sns.swarmplot
(
参数参考sns.stripplot,没有jitter
)
plt.figure()
sns.swarmplot(x = 'MSSubClass',y = 'SalePrice',data = train_data)
plt.savefig('C:/Users/lifeisshort/Desktop/temp.jpg')
6.箱线图
sns.boxplot
(
x = ‘Pclass’
y = ‘Age’
hue = ‘Sex’
data = train_data
)
#同时存在x,y时表示在x特征的的作用分布下,y特征的箱线图
#只存在x,y一个时,仅表示单个特征的分布,x,y表示箱线图的方向
plt.figure()
sns.boxplot(x = 'MSSubClass',y = 'SalePrice',data = train_data,)
plt.savefig('C:/Users/lifeisshort/Desktop/temp.jpg')
关于箱线图的理解:
7.回归图
作用于类别特征时效果奇佳!
7.1使用sns.regplot #不可以使用hue
sns.regplot
(
x = ‘length’
y = ‘hight’
data = data_all
)
7.2使用sns.Implot #可以使用hue
sns.Implot
(
x = ‘length’
y = ‘hight’
data = data_all
hue = ‘sex’
color = ‘g’
marker=’+’ / [’*’,’+’,‘o’]
)
plt.figure()
sns.regplot(x = 'GarageArea',y = 'SalePrice',data = train_data)
plt.savefig('C:/Users/lifeisshort/Desktop/temp.jpg')
8.热力图
通常用来寻找相关性强的特征
这里只介绍简单的操作,入门级别
首先你需要corr = train_data.corr()
sns.heatmap(
corr,
annot = True,
vmax = 0~1 右边颜色柱的最大值(大于等于vmax的数值视为1)(颜色从vmin到vmax开始)
vmin = 0~1 小于边颜色柱的最大值(小于等于vmax的数值视为0)
)
plt.figure()
temp = train_data[['SalePrice','GarageArea','GarageCars']]
corr = temp.corr()
sns.heatmap(corr,annot = True)
plt.savefig('C:/Users/lifeisshort/Desktop/temp.jpg')
进阶操作,大数据怎么办,如果有一万个特征怎么办?
k = 10 #number of variables for heatmap
cols = corr.nlargest(k, 'SalePrice')['SalePrice'].index
corr.nlargest(k, ‘SalePrice’)#相关性表格,返回k个与SalePrice相关性最大的特征的相关性矩阵。
cm = np.corrcoef(train_data[cols].values.T)
hm = sns.heatmap(cm,annot = True)
plt.show()
np.corrcoef(train_data[cols].values.T)计算皮尔逊相关性矩阵