Python绘图库,能做到极致简洁也只有这款了

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

原文链接如下:

Python绘图库,能做到极致简洁也只有这款了

Seaborn 是什么

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

官方链接为
Seaborn官方链接

Seaborn 提供的功能如下:

  • 面向数据集的API,用于检查多个变量之间的关系

  • 专门支持使用分类变量显示观察结果或汇总统计信息

  • 可视化单变量或双变量分布以及在数据子集之间进行比较的选项

  • 不同种类因变量的线性回归模型的自动估计和绘图

  • 用于构造多图网格的高级抽象,可让您轻松构建复杂的可视化

  • 带有几个内置主题的 matplotlib图形样式的精确控制

  • 选择能够忠实显示数据中图案的调色板的工具

Seaborn 安装

#方法1
pip install seaborn
#方法2
conda install seaborn
#方法3
pip install git+https://github.com/mwaskom/seaborn.git

图列展示

1、散点图矩阵

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

在这里插入图片描述

2、小提琴图

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

在这里插入图片描述

3、箱线图

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

在这里插入图片描述

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)

在这里插入图片描述

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

在这里插入图片描述

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学习与数据挖掘』

为方便技术交流,本号开通了技术交流群,有问题请添加小助手微信号:connect_we,备注:加群来自CSDN,欢迎转载,收藏,码字不易,喜欢文章就点赞一下!谢啦
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_38037405/article/details/108294253