天天看点

python绘图库seaborn_Python绘图库:Seaborn 介绍

前言

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。

以下文章来源于Python学习与数据挖掘,作者:喜欢就关注呀

提到Python的图形可视化库,估计你会想到Matplotlib、pyechart、Plotly等,但 Seaborn 却相对低调了许多。最近在做可视化作图中,发现 Seaborn 许多复杂的图形只需一行代码就可以搞定,将作图做到极致简洁,不愧是一款低调却非常有实力的可视化库。

Seaborn 是什么

Seaborn 是一个基于matplotlib的高级可视化效果库,主要针对数据挖掘和机器学习中的变量特征选取,Seaborn 可以用短小的代码去绘制描述更多维度数据的可视化效果图。即便是没有什么基础的人,也可以通过极简的代码,做出具有分析价值而又十分美观的图形。

官方链接为: Seaborn官方链接[1]

Seaborn 提供的功能如下:

•面向数据集的API,用于检查多个变量之间的关系•专门支持使用分类变量显示观察结果或汇总统计信息•可视化单变量或双变量分布以及在数据子集之间进行比较的选项•不同种类因变量的线性回归模型的自动估计和绘图•用于构造多图网格的高级抽象,可让您轻松构建复杂的可视化•带有几个内置主题的 matplotlib图形样式的精确控制•选择能够忠实显示数据中图案的调色板的工具

seaborn优点

优点

•简化了复杂数据集的表示;•可以轻松构建复杂的可视化,简洁的控制matplotlib图形样式与几个内置主题;•seaborn不可以替代matplotlib,而是matplotlib的很好补充;•对于初学者来说容易上手,具有极简模式;

图列展示

1、散点图矩阵

sns.pairplot(iris,hue="species", palette="Set2", diag_kind="kde", height=2.5)

python绘图库seaborn_Python绘图库:Seaborn 介绍

2、小提琴图

sns.violinplot(x="day", y="total_bill", hue="smoker",split=True, inner="quart",palette={"Yes": "y", "No": "b"},data=tips)

python绘图库seaborn_Python绘图库:Seaborn 介绍

3、箱线图

sns.catplot(x="color", y="price", kind="boxen",data=diamonds.sort_values("color"));

python绘图库seaborn_Python绘图库:Seaborn 介绍

4、线性图

# 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)

python绘图库seaborn_Python绘图库:Seaborn 介绍

5、自定义投影的FacetGrid

import numpy as np

import pandas as pd

import seaborn as sns

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")

python绘图库seaborn_Python绘图库:Seaborn 介绍

6、树形图

import pandas as pd

import seaborn as sns

sns.set()

# Load the brain networks example dataset

df = sns.load_dataset("brain_networks", header=[0, 1, 2], index_col=0)

# Select a subset of the networks

used_networks = [1, 5, 6, 7, 8, 12, 13, 17]

used_columns = (df.columns.get_level_values("network")

.astype(int)

.isin(used_networks))

df = df.loc[:, used_columns]

# Create a categorical palette to identify the networks

network_pal = sns.husl_palette(8, s=.45)

network_lut = dict(zip(map(str, used_networks), network_pal))

# Convert the palette to vectors that will be drawn on the side of the matrix

networks = df.columns.get_level_values("network")

network_colors = pd.Series(networks, index=df.columns).map(network_lut)

# Draw the full plot

sns.clustermap(df.corr(), center=0, cmap="vlag",row_colors=network_colors, col_colors=network_colors,linewidths=.75, figsize=(13, 13))

python绘图库seaborn_Python绘图库:Seaborn 介绍

PS:如有需要Python学习资料的小伙伴可以加下方的群去找免费管理员领取

python绘图库seaborn_Python绘图库:Seaborn 介绍

可以免费领取源码、项目实战视频、PDF文件等

python绘图库seaborn_Python绘图库:Seaborn 介绍

内容来源于网络如有侵权请私信删除