数据可视化: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()
相关推荐
有来技术几秒前
vite-plugin-vue-mcp:在 Vue 3 + Vite 中启用 MCP,让 AI 理解并调试你的应用
前端·vue.js·人工智能
该用户已不存在1 分钟前
免费的 Vibe Coding 助手?你想要的Gemini CLI 都有
人工智能·后端·ai编程
闲人编程30 分钟前
用Python分析你的Spotify/网易云音乐听歌数据
开发语言·python·ai·数据分析·spotify·网易云·codecapsule
“负拾捌”1 小时前
LangChain 中 ChatPromptTemplate 的几种使用方式
python·langchain·prompt
thorn_r1 小时前
MCP驱动的AI角色扮演游戏
人工智能·游戏·机器学习·ai·自然语言处理·agent·mcp
得贤招聘官1 小时前
智能招聘革新:破解校招低效困局的核心方案
人工智能
乌恩大侠1 小时前
【Spark】操作记录
人工智能·spark·usrp
一水鉴天1 小时前
整体设计 全面梳理复盘 之27 九宫格框文法 Type 0~Ⅲ型文法和 bnf/abnf/ebnf 之1
人工智能·状态模式·公共逻辑
极客BIM工作室1 小时前
GAN vs. VAE:生成对抗网络 vs. 变分自编码机
人工智能·神经网络·生成对抗网络