Seaborn 入门使用

Seaborn 是一个基于 matplotlib 的 Python 数据可视化库。它提供了一个高级界面,用于绘制有吸引力且信息丰富的统计图形。 官网地址

安装

pip3 install matplotlib

pip3 install Seabrn

要先安装 matplotlib

入门例子

python 复制代码
import seaborn as sns
import matplotlib.pyplot as plt
​
# 生成一个柱形图
sns.barplot(x=["A", "B", "C"], y=[1, 3, 2])
# 生成图像
plt.show()

效果如下

可以使用 set_theme() 添加主题(背景色,字体颜色,字体大小等等)。

在 set_theme() 有多个参数可以定制化主题,如果没有传入参数也会有默认的主题

示例

python 复制代码
import seaborn as sns
import matplotlib.pyplot as plt
​
sns.set_theme()
# 生成一个柱形图
sns.barplot(x=["A", "B", "C"], y=[1, 3, 2])
plt.show()

与第一个图相比样式明显有了很大的变化

set_theme()

官网 set_theme() 的api 官网上介绍了参数的使用

如下示例改变了轴的样式

dart 复制代码
import seaborn as sns
import matplotlib.pyplot as plt
​
​
sns.set_theme(style="ticks")
sns.barplot(x=["A", "B", "C"], y=[1, 3, 2])
​
plt.show()

参数 style 可以改变坐标轴的样式,如果使用的时传入的参数是 str

就只有固定的几个参数 white、 dark、 whitegrid、 darkgrid、ticks 使用别的会报错

dart 复制代码
import seaborn as sns
import matplotlib.pyplot as plt
​
sns.set_theme(style="whitegrid")
sns.barplot(x=["A", "B", "C"], y=[1, 3, 2])
​
plt.show()

rc 参数可以覆盖任何 seaborn 参数(包括之前设置的),还能使用属于 matplotlib rc 系统但未包含在 seaborn 主题中的其他参数

如下展示了将桌标轴都去掉的效果

ini 复制代码
import seaborn as sns
import matplotlib.pyplot as plt
​
custom_params = {"axes.spines.right": False, "axes.spines.top": False, "axes.spines.bottom": False, "axes.spines.left": False}
sns.set_theme(style="whitegrid", rc=custom_params)
sns.barplot(x=["A", "B", "C"], y=[1, 3, 2])
​
plt.show()
相关推荐
叶智辽19 分钟前
Three.js多视口渲染:如何在一个屏幕上同时展示三个视角
webgl·three.js·数据可视化
mCell2 天前
分享一个常用的文生图提示词
人工智能·llm·数据可视化
玄魂3 天前
Coze+ VisActor Skill:智能图表,触手可及
ai编程·数据可视化·coze
柳杉6 天前
使用Ai从零开发智慧水利态势感知大屏(开源)
前端·javascript·数据可视化
ANQH8 天前
Manim三大核心类详解
数据可视化
柳杉11 天前
使用AI从零打造炫酷医疗数据可视化大屏,源码免费拿!
前端·javascript·数据可视化
Highcharts.js11 天前
Highcharts热力图(Heatmap)完全指南:从基础配置到地理热力图,一文学会颜色轴数据可视化
信息可视化·数据可视化·热力图·heatmap·highcharts·地理热力图
凌云拓界12 天前
TypeWell全攻略(二):热力图渲染引擎,让键盘发光
前端·后端·python·计算机外设·交互·pyqt·数据可视化
柳杉13 天前
使用AI从零打造炫酷的智慧城市大屏(开源):React + Recharts 实战分享
前端·javascript·数据可视化
凌云拓界13 天前
TypeWell全攻略:AI健康教练+实时热力图开发实战 引言
前端·人工智能·后端·python·交互·pyqt·数据可视化