前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python简单高效的可视化神器——Seaborn

Python简单高效的可视化神器——Seaborn

作者头像
MeteoAI
发布2019-07-22 18:25:25
2.5K0
发布2019-07-22 18:25:25
举报
文章被收录于专栏:MeteoAIMeteoAI

前面我们已经介绍了matplotlib的一些基础和进阶的操作,相信大家已经掌握了。没有掌握的同学快回去学一学!

如何用python画图——带你入门matplotlib

如何用python画图--matplotlib实例与补充

我们也在文章Python可视化工具概览中介绍了,seaborn其实是在matplotlib基础上进行了更高级的封装,使得一些出图更加的快捷方便而且美观。

seaborn对matplotlib和pandas的数据结构高度兼容 ,非常适合用于数据的可视化分析。seaborn官网:http://seaborn.pydata.org/

好了,下面我们就正式开始了,依旧是从实例开始带大家轻松愉快入门~

代码语言:javascript
复制
import seaborn as sns # 导入 seaborn 模块
sns.set() # 使用set() 1步设置默认样式

# 切换回matplotlib:
# sns.reset_orig() ## reset back to the matplotlib

# 默认参数:
seaborn.set(context='notebook', style='darkgrid', 
                   palette='deep', font='sans-serif', 
                   font_scale=1, color_codes=True, rc=None)

其中:

  • context='' 参数控制着默认的画幅大小,分别有 {paper, notebook, talk, poster} 四个值。其中,poster > talk > notebook > paper
  • style='' 参数控制默认样式,分别有 {darkgrid, whitegrid, dark, white, ticks},你可以自行更改查看它们之间的不同。
  • palette='' 参数为预设的调色板。分别有 {deep, muted, bright, pastel, dark, colorblind} 等,你可以自行更改查看它们之间的不同。
  • font='' 用于设置字体,
  • font_scale= 设置字体大小,
  • color_codes= 不使用调色板而采用先前的 'r' 等色彩缩写。

0. appetizer

代码语言:javascript
复制
## appetizer:
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
sns.set()

x = np.linspace(0,20,8)
y_bar = np.random.randint(5,20,8)
y_line = y_bar 

plt.figure(figsize=(8,6))
plt.bar(x, y_bar, color='r')
plt.plot(x, y_line, '-o', color='g')
plt.tick_params(labelsize='15', width=1)

plt.show()

1. lmplot

代码语言:javascript
复制
import seaborn as sns
sns.set()

## load dataset
iris_data = sns.load_dataset('iris')  # 导入iris数据集做实验
iris_data.head()  # 预览该数据集

# plot:
# lmplot: 可以绘制原始数据、线性拟合线及其置信区间
sns.set(font_scale = 1.6)
sns.lmplot(x='sepal_length', y='sepal_width', hue='species', data=iris_data, height=6, aspect=1.5)
# FYI: https://seaborn.pydata.org/generated/seaborn.lmplot.html
plt.show()

sns.lmplot() 里的 x, y 分别代表横纵坐标的列名。hue= 代表按照 species,即花的类别分类显示,data= 即为数据集。

2. distplot and kdeplot

seaborn.distplot() 主要用于查看单变量的分布状况。

代码语言:javascript
复制
## distplot: 单变量分布的可视化
plt.figure(figsize = (8, 6))
sns.set(font_scale = 1.6)
x = np.random.randn(100)
sns.distplot(x,kde=True,rug=True,hist=True)
plt.show()

seaborn.kdeplot() 主要用于绘制单变量或二元变量的核密度估计图,可以查看变量的分布状况。

代码语言:javascript
复制
## kdeplot:single variable
plt.figure(figsize = (8, 6))
sns.kdeplot(data=iris_data["sepal_length"], shade=True)
# Labeling of plot
# plt.xlabel('X'); plt.ylabel('Y'); plt.title('Distribution');
plt.show()
############################
## kdeplot:double variables
plt.figure(figsize = (8, 6))
sns.kdeplot(data=iris_data["sepal_length"], data2=iris_data["sepal_width"], shade=True)
# Labeling of plot
# plt.xlabel('X'); plt.ylabel('Y'); plt.title('Distribution');
plt.show()

3. heatmap

seaborn.heatmap() 主要是用于绘制热力图,也就类似于色彩矩阵。常用于相关系数矩阵的可视化,评估变量之间的相关性。

代码语言:javascript
复制
## Heatmap
sns.set(font_scale=1.5)
plt.figure(figsize = (10, 10))
# 生成 10x10 的随机矩阵
matrix_data = np.random.randn(10, 10)
plt.title('Heatmap');
sns.heatmap(matrix_data, cmap = plt.cm.RdYlBu_r, fmt='.2f', vmin = -0.6, annot = True, vmax = 0.6)
# FYI: https://seaborn.pydata.org/generated/seaborn.heatmap.html?highlight=heatmap#seaborn.heatmap
plt.show()

4. JointGrid() and jointplot

seaborn.JointGrid() 可以绘制单变量和双变量的组合图。

代码语言:javascript
复制
# load data
iris_data = sns.load_dataset('iris')
# 我们将中间的图选为双变量的散点图,而上面和右面选为单变量的直方图
sns.JointGrid(data=iris_data, x='sepal_length', y='sepal_width',height=10).plot(sns.regplot, sns.distplot)
# FYI: https://seaborn.pydata.org/generated/seaborn.JointGrid.html?highlight=jointgrid
plt.show()

seaborn.jointplot()seaborn.JointGrid()更高级的封装,使用更加灵活。

代码语言:javascript
复制
## jointplot:
sns.set(font_scale=1.6)
sns.jointplot(x='sepal_length', y='sepal_width', height=8, data=iris_data, kind="reg")
plt.show()

5. PairGrid and pairplot

seaborn.PairGrid() 可以用来查看两个维度数据之间的关系。

代码语言:javascript
复制
## PairGrid
# load data
iris_data = sns.load_dataset('iris')
# plot
sns.PairGrid(data=iris_data, hue='species').map(plt.scatter)
# FYI: https://seaborn.pydata.org/generated/seaborn.PairGrid.html?highlight=pairgrid
plt.show()

seaborn.pairplot()seaborn.PairGrid()更高级的封装,使用更加灵活。

代码语言:javascript
复制
## pairplot
sns.set(style="ticks", color_codes=True,font_scale=1.5) 
iris = sns.load_dataset("iris")
g = sns.pairplot(iris,hue='species', height=3, diag_kind='kde',diag_kws=dict(shade=True),plot_kws=dict(s=40))
# FYI: https://seaborn.pydata.org/generated/seaborn.pairplot.html?highlight=pairplot#seaborn.pairplot

6. boxplot

代码语言:javascript
复制
## boxplot
## load data:
tips = sns.load_dataset("tips")
## plot:
sns.set(font_scale=1.5)
plt.figure(figsize = (12, 8))
sns.boxplot(x="day", y="total_bill", hue="smoker",data=tips, palette="Set3")
# FYI : https://seaborn.pydata.org/generated/seaborn.boxplot.html?highlight=boxplot#seaborn.boxplot

7. catplot

seaborn.catplot()可以用来查看数值型变量和一个或多个类别型变量的关系。具体内容还是强烈建议大家查阅官网。

其提供的kind参数可以做很多画图类型的选择:

代码语言:javascript
复制
# Categorical scatterplots:
stripplot() (with kind="strip"; the default)
swarmplot() (with kind="swarm")
# Categorical distribution plots:
boxplot() (with kind="box")
violinplot() (with kind="violin")
boxenplot() (with kind="boxen")
# Categorical estimate plots:
pointplot() (with kind="point")
barplot() (with kind="bar")
countplot() (with kind="count")
代码语言:javascript
复制
# catplot 
"""
It can also be useful to combine swarmplot() or striplot() with a box plot or violin plot 
to show each observation along with a summary of the distribution:
"""
sns.set(font_scale=1.5) 
g = sns.catplot(x="day", y="total_bill", kind="violin", inner=None, data=tips,height=7,aspect=1.6)
sns.swarmplot(x="day", y="total_bill", color="k", size=5, data=tips, ax=g.ax);


本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-11-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 MeteoAI 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 0. appetizer
  • 1. lmplot
  • 2. distplot and kdeplot
  • 3. heatmap
  • 4. JointGrid() and jointplot
  • 5. PairGrid and pairplot
  • 6. boxplot
  • 7. catplot
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档