Seaborn 基础知识与应用案例精讲
基础知识
安装
pip install seaborn
导入
import seaborn as sns
基本设置
- 图表风格:Seaborn 提供了五种风格,分别是
darkgrid
、whitegrid
、dark
、white
、ticks
。sns.set(style="whitegrid")
- 颜色主题:Seaborn 有多种颜色主题可供选择,如
deep
、muted
、pastel
、bright
等。sns.set_palette("muted")
- 字体与中文支持:
sns.set(font='SimHei', font_scale=1.5)
plt.rcParams['axes.unicode_minus'] = False
常用图表类型
- 关系图:用于展示变量之间的关系。
relplot()
:关系图的接口,通过指定 kind
参数可以画出散点图和折线图。
scatterplot()
:散点图。
lineplot()
:折线图。
- 分类图:用于展示分类数据。
catplot()
:分类图的接口,通过指定 kind
参数可以画出多种分类图。
barplot()
:条形图。
countplot()
:计数图。
boxplot()
:箱型图。
violinplot()
:小提琴图。
- 分布图:用于展示数据的分布情况。
distplot()
:直方图。
kdeplot()
:核密度估计图。
rugplot()
:将数组中的数据点绘制为轴上的数据。
- 回归图:用于展示变量之间的回归关系。
lmplot()
:回归模型图。
regplot()
:线性回归图。
- 矩阵图:用于展示矩阵数据。
heatmap()
:热力图。
clustermap()
:聚集图。
应用案例
案例 1:鸢尾花数据集的散点图
import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset("iris")
sns.scatterplot(x="sepal_length", y="sepal_width", hue="species"