数据可视化:Seaborn

安装Seaborn

进入虚拟环境,在终端中键入

pip install seaborn

即可安装。

初步使用Seaborn

在使用seaborn之前,我们先了解一下seaborn是什么,seaborn是以matplotlib为底层的更简便的python第三方库,它可以更快捷地去设置图形的一些参数,让图表元素改变比例,颜色,背景等等的工具包,使用Seaborn可以给我们美化的图表。

一,风格设置

除了各种绘图方式外,图形的美观程度可能是我们最关心的了。将它放到第一部分,因为风格设置是一些通用性的操作,对于各种绘图方法都适用。

Seaborn 支持的风格有5种:

  • darkgrid 黑背景-白格
  • whitegrid 白背景-白格
  • dark 黑背景
  • white 白背景
  • ticks

接下来,我们将用代码演示一下各自的效果。

python 复制代码
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np


#seaborn风格设置
y = np.random.randint(50,100,20)
x = range(20)
sns.set_style('white')
plt.subplot(231)
plt.plot(x,y)
sns.set_style('dark')
plt.subplot(232)
plt.plot(x,y)
sns.set_style('whitegrid')
plt.subplot(233)
plt.plot(x,y)
sns.set_style('darkgrid')
plt.subplot(234)
plt.plot(x,y)
sns.set_style('ticks')
plt.subplot(235)
plt.plot(x,y)


plt.show()

二,移除轴脊柱

white和ticks两个风格都能够移除顶部和右侧的不必要的轴脊柱。通过matplotlib参数是做不到这一点的,但是你可以使用 seaborn的despine() 方法来移除它们

python 复制代码
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np


#seaborn风格设置
y = np.random.randint(50,100,20)
x = range(20)
sns.set_style('white')
plt.subplot(231)
plt.plot(x,y)
sns.set_style('dark')
plt.subplot(232)
plt.plot(x,y)
sns.set_style('whitegrid')
plt.subplot(233)
plt.plot(x,y)
sns.set_style('darkgrid')
plt.subplot(234)
plt.plot(x,y)
sns.set_style('ticks')
plt.subplot(235)
plt.plot(x,y)
sns.despine()



plt.show()

三,图像风格管理

简单来说,这就是修改图像的规格:粗细,大小等属性,我们调用seanborn.set_context()方法来设置。

同样地,我们使用代码来演示效果:

python 复制代码
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

x = range(10)
y = np.random.randint(10,100,10)

#调用set_context来设置
plt.subplot(221)
plt.plot(x,y)
sns.set_context("paper")
plt.subplot(222)
plt.plot(x,y)
sns.set_context("notebook")
plt.subplot(223)
plt.plot(x,y)
sns.set_context("talk")
plt.subplot(224)
plt.plot(x,y)
sns.set_context("poster")

plt.show()
相关推荐
nightunderblackcat21 分钟前
新手向:异步编程入门asyncio最佳实践
前端·数据库·python
weixin_4486170524 分钟前
疏老师-python训练营-Day54Inception网络及其思考
python
windSnowLi26 分钟前
Python opencv识别图片中重叠圆的圆心位置
开发语言·python·opencv
井云AI36 分钟前
井云智能体封装小程序:独立部署多开版 | 自定义LOGO/域名,打造专属AI智能体平台
人工智能·后端·小程序·前端框架·coze智能体·智能体网站·智能体小程序
张较瘦_44 分钟前
[论文阅读] 人工智能 + 软件工程 | 技术债务管理新范式:五步法工作坊与行动研究实践
人工智能·软件工程
杨杨杨大侠1 小时前
Spring AI 系列(一):Spring AI 基础概念与架构入门
人工智能·spring·架构
人大博士的交易之路1 小时前
龙虎榜——20250822
大数据·数据挖掘·数据分析·缠中说禅·龙虎榜·道琼斯结构
nenchoumi31191 小时前
手持 Mid360 + RealSense 相机 + Jetson Orin 一体平台
人工智能·目标检测·计算机视觉·机器人·ros
XXD啊1 小时前
Redis 从入门到实践:Python操作指南与核心概念解析
数据库·redis·python
大力财经2 小时前
百度Q2财报:总营收327亿 AI新业务收入首次超100亿
人工智能·百度