用python的seaborn画好看的图

这篇文章是做实验楼的一个seaborn的实验,做的记录。

实验网址:https://www.lanqiao.cn/courses/892/learning/?id=3260

简介

seaborn是基于python下一个很优秀的画图的库matplotlib的进阶封装,主要在配色上更漂亮。下面这张图是matplotlib的。
下面的代码是用matplotlib画一个条形图和折线图。

import matplotlib.pyplot as plt
%matplotlib inline

x = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
y_bar = [3, 4, 6, 8, 9, 10, 9, 11, 7, 8]
y_line = [2, 3, 5, 7, 8, 9, 8, 10, 6, 7]

plt.bar(x, y_bar)
plt.plot(x, y_line, '-o', color='y')

在这里插入图片描述

在画图前加上sns.set()就可以用seaborn美化matplotlib的图:

import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
x = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
y_bar = [3, 4, 6, 8, 9, 10, 9, 11, 7, 8]
y_line = [2, 3, 5, 7, 8, 9, 8, 10, 6, 7]

plt.bar(x, y_bar)
plt.plot(x, y_line, '-o', color='y')

在这里插入图片描述

关联图

关联图是分析两类数据的之间的关系,也是比较常用的。seaborn提供了三个API画这个图:

  • relplot
  • scatterplot
  • lineplot

relplot是relation plot的缩写。主要有散点图和条形图。下面用鸢尾花数据集来画个散点图。

import seaborn as sns
sns.set()
iris = sns.load_dataset("iris")
sns.relplot(x="sepal_length", y="sepal_width", data=iris)

在这里插入图片描述
加入类别这个维度~

sns.relplot(x="sepal_length",y="sepal_width",hue='species',data=iris)

在这里插入图片描述
不同类别显示不一样的形状~

sns.relplot(x="sepal_length",y="sepal_width",hue='species',data=iris,style="species")

在这里插入图片描述
在kind处设置为scatter或者line

sns.relplot(x="sepal_length",y="sepal_width",hue="species",style="species",kind="scatter",data=iris)
sns.relplot(x="sepal_length",y="sepal_width",hue="species",style="species",kind="line",data=iris)

在这里插入图片描述

类别图

类别图的接口是catplot(),类别图主要是用来看不同类别的数据情况。
主要分为以下三种:

  • 分类散点图
  • 分类分布图
  • 分类估计图

分类散点图

画一个分类散点图,自变量为连续的值,因变量为种类这一离散的值。

sns.catplot(x="sepal_length", y="species", data=iris)

在这里插入图片描述
在kind参数设置swarm,更好地看数据分布。

sns.catplot(x="sepal_length", y="species", kind="swarm", data=iris)

在这里插入图片描述

分类分布图

绘制箱线图

sns.catplot(x="sepal_length",y="species",kind="box",data=iris)

在这里插入图片描述
绘制小提琴图,虽然并没有看出哪里像小提琴。

sns.catplot(x="sepal_length",y="species",kind="violin",data=iris)

在这里插入图片描述
绘制增强箱线图

sns.catplot(x="species", y="sepal_length", kind="boxen", data=iris)

在这里插入图片描述

分类估计图

绘制点线图

sns.catplot(x="sepal_length", y="species", kind="point", data=iris)

在这里插入图片描述
绘制条型图

sns.catplot(x="sepal_length", y="species", kind="bar", data=iris)

在这里插入图片描述
绘制计数条形图,可以看到这三类数据个数都为50

sns.catplot(x="species", kind="count", data=iris)

在这里插入图片描述

分布图

单变量分布

sns.distplot(iris["sepal_length"],kde=True,hist=False)
#等价于下面注释的方法
#sns.kdeplot(iris["sepal_length"])

在这里插入图片描述

sns.distplot(iris["sepal_length"],kde=False,hist=True)

在这里插入图片描述

二元变量分布

sns.jointplot(x="sepal_length",y="sepal_width",data=iris)
#等价于下面注释的方法
#sns.jointplot(x="sepal_length",y="sepal_width",data=iris,kind="scatter")

在这里插入图片描述

sns.jointplot(x="sepal_length",y="sepal_width",data=iris)
#等价于下面注释的方法
#sns.jointplot(x="sepal_length",y="sepal_width",data=iris,kind="kde")

在这里插入图片描述

sns.jointplot(x="sepal_length", y="sepal_width", data=iris,kind="hex")

在这里插入图片描述

sns.jointplot(x="sepal_length", y="sepal_width", data=iris,kind="reg")

在这里插入图片描述
无敌的pairplot

sns.pairplot(iris,hue="species")

在这里插入图片描述

回归图

  • regplot
  • lmplot (支持hue)
sns.regplot(x="sepal_length", y="sepal_width", data=iris)

在这里插入图片描述

sns.lmplot(x="sepal_length", y="sepal_width", data=iris,hue="species")

在这里插入图片描述

矩阵图

到这边我已经看不懂这图画的到底是啥了=_=

  • heatmap(热力图)
  • clustermap (层次聚类图)
import numpy as np
sns.heatmap(np.random.rand(5,5))

在这里插入图片描述

iris.pop("species")#把标签弹出去,
sns.clustermap(iris)#然后进行层次聚类

在这里插入图片描述

总结

在这里插入图片描述

参考链接

  • seaborn官方文档:https://seaborn.pydata.org/api.html
  • 实验楼seaborn数据可视化基础入门:https://www.lanqiao.cn/courses/892/learning/?id=3260
  • 3
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

破落之实

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值