Python数据可视化库 Seaborn

 

Seaborn简介

http://seaborn.pydata.org/

seaborn其实是在matplotlib的基础上进行了更高级的API封装,从而使得作图更加容易,在大多数情况下使用seaborn就能做出很具有吸引力的图。

seaborn是针对统计绘图的,一般来说,seaborn能满足数据分析90%的绘图需求,够用了,如果需要复杂的自定义图形,还是要Matplotlib。我们来看下seaborn 可以帮助绘制那些可视化图,这里截取了部分的效果,

 

Seaborn 的安装

安装最新的seaborn 的版本,我们可以通过使用pip 命令:

pip install seaborn

如果你已经安装conda,可以使用下面的命令进行seaborn 安装(推荐)

conda install seaborn

安装成功后,我们需要检查seaborn 的安装是否成功,可以通过导入seaborn 相关的包,来检查下。

In [2]:

import seaborn as sns
sns.__version__

Out[2]:

'0.9.0'

我们在数据可视化分析的时候,我们seaborn 来操作需要导入相关的包

In [85]:

# 我们导入seaborn和matplotlib 库,分别重新命名 分别sns 和plt
import seaborn as sns 
import matplotlib.pyplot as plt
# jupyter-notebook 中图形可以嵌入方式展示处理
%matplotlib inline 
# 去除一些警告信息
import warnings
warnings.filterwarnings('ignore')

# pandas 数据分析包
# numpy 数值计算的库
import numpy as np
import pandas as pd

 

可视化 distplot 直方图

 

titanic = pd.read_csv('data/titanic.csv')
titanic.head()
survived	pclass	sex	age	sibsp	parch	fare	embarked	class	who	adult_male	deck	embark_town	alive	alone
0	0	3	male	22.0	1	0	7.2500	S	Third	man	True	NaN	Southampton	no	False
1	1	1	female	38.0	1	0	71.2833	C	First	woman	False	C	Cherbourg	yes	False
2	1	3	female	26.0	0	0	7.9250	S	Third	woman	False	NaN	Southampton	yes	True
3	1	1	female	35.0	1	0	53.1000	S	First	woman	False	C	Southampton	yes	False
4	0	3	male	35.0	0	0	8.0500	S	Third	man	True	NaN	Southampton	no	True

在分析一组数据的时候,看的就是变量分布规律,而直方图提供这种简单的快速操作,在seaborn中可以使用distplot() 实现

https://seaborn.pydata.org/generated/seaborn.distplot.html?highlight=distplot#seaborn.distplot

通过观察数据,对泰坦尼克号 age 进行直方图展示,实际age 字段存在缺失数值,那么这种情况下,我们首先 对缺失的数据进行数据 ,通过pandas 提供dropna() 删除nan的数据,否则无法绘制我们的直方图图形。

In [29]:

row,col = titanic.shape
print("row is {}".format(row))
print("col is {}".format(col))
row is 891
col is 15

In [27]:

titanic.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 15 columns):
survived       891 non-null int64
pclass         891 non-null int64
sex            891 non-null object
age            714 non-null float64
sibsp          891 non-null int64
parch          891 non-null int64
fare           891 non-null float64
embarked       889 non-null object
class          891 non-null object
who            891 non-null object
adult_male     891 non-null bool
deck           203 non-null object
embark_town    889 non-null object
alive          891 non-null object
alone          891 non-null bool
dtypes: bool(2), float64(2), int64(4), object(7)
memory usage: 92.3+ KB

In [31]:

age = titanic['age'].dropna()
type(age)

Out[31]:

pandas.core.series.Series

In [32]:

sns.distplot(age)

 

在上图中,矩形表示不同的年龄的数据分布的情况,并且distplot() 默认情况下,拟合出来的密度曲线,可以看出分布的变化规律

同时,我们可以调节一些重要的参数,来控制输出我们的图形展示效果

 

# 去掉拟合的密度估计的曲线,kde = False . 默认情况: kde = True
sns.distplot(age,kde=False)

 

bins 是控制分布矩形的数量的参数,通过可以修改数量,来查看我们数据的效果

In [37]:

sns.distplot(age,bins=30,kde = False)

Out[37]:

<matplotlib.axes._subplots.AxesSubplot at 0x1a23ddd400>

 

通过更丰富的参数来控制我们的展示细节问题,例如: hist_kws,kde_kws 来设置。

 

# 可以直方图、密度图的关键参数
fig,axes = plt.subplots(1,2)
sns.distplot(age,ax = axes[0])

sns.distplot(age,
             hist_kws={'color':'green','label':'hist'},
             kde_kws={'color':'red','label':'KDE'},
             ax=axes[1])

 

 

Python 数据可视化库Seaborn 主要知识点如下:

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艾文教编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值