温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python中Seaborn库如何使用

发布时间:2021-07-10 14:04:57 来源:亿速云 阅读:211 作者:Leah 栏目:大数据

Python中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库如何使用

2、小提琴图

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

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)

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库如何使用

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

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI