数据可视化: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()
相关推荐
用户3721574261352 分钟前
使用 Python 压缩 PDF 文件:减小图片、字体和文档内容体积
python
酒旅Agent开发实战4 分钟前
酒店供应链MCP实践分享
人工智能·大模型·酒店预订·ai agent·mcp
java1234_小锋6 分钟前
Tornado 6.5,全面支持 Python 3.14
数据库·python·tornado
xsd202411189 分钟前
篮球排球足球目标跟踪全解析:从多目标跟踪算法到球轨迹预测的技术
人工智能·目标跟踪
动恰客流统计19 分钟前
传统红外对射客流统计为何逐步淡出主流?准确率与场景限制深度分析
大数据·前端·人工智能
Theo_xx20 分钟前
声学感知基础:Day3(2).STFT(短时傅里叶变换)与ToF(飞行时间)
人工智能·无线感知·声学感知
IT毕设梦工厂20 分钟前
计算机毕业设计选题推荐:基于大数据的印度上市公司财务指标数据可视化分析|毕业设计选题|计算机毕设|选题推荐|毕设指导|项目定制|源码|高质量项目
大数据·hadoop·python·信息可视化·spark·课程设计·大数据毕设项目
SEO_juper21 分钟前
你的服务器正在被 AI 爬虫“白嫖“带宽:2026 用日志把 Googlebot 和 AI 洪流分开算账(附脚本)
运维·人工智能·爬虫·python·chatgpt·seo
浩风祭月21 分钟前
GPT Image 2.5 Sunburst还是Flare?模型选型、Python接入与成本避坑
python·aigc·openai·图像生成·gpt image 2.5