python绘图库seaborn_python绘图库——Matplotlib及Seaborn使用(入门篇2)

在数据分析过程中,数据及模型可视化是无可避免的,同时这也是展示我们数据分析成果的最佳方式。因此,熟悉掌握绘图库的使用,对精进我们的数据分析技能起着不可替代的作用。

在上一篇中,我们掌握了Matplotlib的基本操作技巧。在有一定的认识基础后,我们今天再来看看在python里另一个强大的绘图库——Seaborn。

重温一下:

Seaborn 是以 Matplotlib 为核心的高阶绘图库,它基于 Matplotlib 核心库进行了更高阶的 API 封装,可以让你轻松地画出更漂亮的图形。Seaborn 的漂亮主要体现在配色更加舒服、以及图形元素的样式更加细腻。

下面我们来具体了解一下Seaborn有哪些优点:

1、帮助我们快速地优化图形(让我们画的图更美观)

首先我们借助上一篇的到代码,先来看看不用seaborn库的图形效果:

还是记得先导入库:

from matplotlib import pyplot as plt

%matplotlib inline

再代入图形的代码:

x=[1,2,3,4,5,6,7]

y1=[3,6,9,12,9,6,3]

y2=[2,9,11,8,6,4,3]

plt.bar(x, y1)

plt.plot(x, y2, '-o', color='r')

下面我们导入seaborn库,看看效果有什么变化:

首先导入库:

import seaborn as sns

sns.set()

再次导入代码:

x=[1,2,3,4,5,6,7]

y1=[3,6,9,12,9,6,3]

y2=[2,9,11,8,6,4,3]

plt.bar(x, y1)

plt.plot(x, y2, '-o', color='r')

对比之下,新画出来的图像颜色更为柔和,背景也较为舒适,整体上美观了不少。

敲小黑板:

在上述代码中,我们使用import seaborn as sns 导入库之后,接着在绘图之前,使用了Seaborn提供的声明代码:sns.set( ) 。

我们来简单认识一下sns.set( ):

函数体:

sns.set(context='notebook', style='darkgrid', palette='deep', font='sans-serif', font_scale=1, color_codes=False, rc=None)

主要参数:

1、context=' '

控制着默认的画幅大小,分别有 {paper, notebook, talk, poster} 四个值。其中,poster > talk > notebook > paper。

2、style=' '

控制默认样式,分别有 {darkgrid, whitegrid, dark, white, ticks},你可以自行更改查看它们之间的不同。

3、palette=' '

参数为预设的调色板。分别有 {deep, muted, bright, pastel, dark, colorblind} 等,你可以自行更改查看它们之间的不同。

4、font=' '

设置字体,font_scale= 设置字体大小,color_codes= 不使用调色板而采用先前的 'r' 等色彩缩写。

除此之外,

Seaborn 的绘图方法主要包括以下六种:关联图

类别图

分布图

回归图

矩阵图

组合图

具体用法:sns.绘图方法

例如:sns.relplot( )

每种绘图类型里,又各自包含了不同的函数,想具体了解的小伙伴可以到seaborn的官网看一下:seaborn: statistical data visualization​seaborn.pydata.org

下面我们简单介绍一下各个图类的一些用法:

关联图:

主要用于对数据进行关联性分析,常见的有以下用法:

要解释一下的是,上述的这种用法,在Seaborn 叫做 API (接口)API(应用程序编程接口)_百度百科​baike.baidu.comv2-98c5941e335960133fab6832155eeb75_180x120.jpg

其中,Seaborn 中的 API 分为 Figure-level 和 Axes-level 两种。

relplot是一个 Figure-level 接口,而scatterplot和lineplot则是 Axes-level 接口。

(Figure-level 和 Axes-level API 的区别?)

Axes-level 的函数可以实现与 Matplotlib 更灵活和紧密的结合,而 Figure-level 使用上更简单,适合快速应用。

类别图

catplot,categorical plots 的缩写。包含了许多Axes-level API,主要包括以下几种:

分布图

用于可视化变量的分布情况,一般分为单变量分布和多变量分布。其中,多变量多指二元变量,更多的变量无法绘制出直观的可视化图形。

常用方法:jointplot

pairplot

distplot

kdeplot

回归图

常用方法:lmplot

regplot

lmplot:支持引入第三维度进行对比,例如设置hue="species"。

regplot:绘制回归图时,只需要指定自变量和因变量,会自动完成线性回归拟合。

矩阵图

常用方法:heatmap

clustermap

heatmap :用于绘制热力图。

clustermap:用于绘制层次聚类结构图

组合图

和Matplotlib的用法差不多~侦探L:python绘图库——Matplotlib及Seaborn使用(入门篇1)​zhuanlan.zhihu.comv2-6956dd00e809f9f23bc2ae78e0f6420b_ipico.jpg

最后,我们来欣赏一下seaborn官网一些好看的图吧~

import seaborn as sns

sns.set(style="ticks")

dots = sns.load_dataset("dots")

# Define a palette to ensure that colors will be

# shared across the facets

palette = dict(zip(dots.coherence.unique(),

sns.color_palette("rocket_r", 6)))

# Plot the lines on two facets

sns.relplot(x="time", y="firing_rate",

hue="coherence", size="choice", col="align",

size_order=["T1", "T2"], palette=palette,

height=5, aspect=.75, facet_kws=dict(sharex=False),

kind="line", legend="full", data=dots)

import numpy as np

import pandas as pd

import seaborn as sns

from matplotlib import pyplot as plt

%matplotlib inline

sns.set()

# Generate an example radial datast

r = np.linspace(0, 10, num=100)

df = pd.DataFrame({'r': r, 'slow': r, 'medium': 2 * r, 'fast': 4 * r})

# Convert the dataframe to long-form or "tidy" format

df = pd.melt(df, id_vars=['r'], var_name='speed', value_name='theta')

# Set up a grid of axes with a polar projection

g = sns.FacetGrid(df, col="speed", hue="speed",

subplot_kws=dict(projection='polar'), height=4.5,

sharex=False, sharey=False, despine=False)

# Draw a scatterplot onto each axes in the grid

g.map(sns.scatterplot, "theta", "r")

以上便是的内容,感谢大家的细心阅读,同时欢迎感兴趣的小伙伴一起讨论、学习,想要了解更多内容的可以看我的其他文章,同时可以持续关注我的动态~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值