python seaborn画图_Python简单高效的可视化神器——Seaborn

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

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

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

好了,下面我们就正式开始了,依旧是从实例开始带大家轻松愉快入门~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## 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. lmplotimport 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() 主要用于查看单变量的分布状况。## 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() 主要用于绘制单变量或二元变量的核密度估计图,可以查看变量的分布状况。## 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() 主要是用于绘制热力图,也就类似于色彩矩阵。常用于相关系数矩阵的可视化,评估变量之间的相关性。## 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() 可以绘制单变量和双变量的组合图。# 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()更高级的封装,使用更加灵活。## 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() 可以用来查看两个维度数据之间的关系。## 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()更高级的封装,使用更加灵活。## 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## 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参数可以做很多画图类型的选择:# 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")# 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);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值