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数据分析,进一步的数据分析,如果大家有兴趣,可以参考一下内容学习:

Python 数据可视化进阶:

1. Python数据可视化教程:基于Plotly的动态可视化绘图
课程内容: https://edu.csdn.net/course/detail/24935

2. Python数据可视化教程 seaborn 
课程内容: https://edu.csdn.net/course/detail/24790

欢迎关注,一起交流讨论!
 

针对Python 数据可视化库 Seaborn 课程的主要功能介绍如下: 

项目实战内容:泰坦尼克号数据、鸢尾花数据、航班数据

课程特点: 多行业实战项目教学 + 同步配套完整代码 + 零基础学习seaborn 数据分析可视化操作

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

部分课程的应用场景效果图如下:

发布了267 篇原创文章 · 获赞 66 · 访问量 43万+

猜你喜欢

转载自blog.csdn.net/shenfuli/article/details/91990488