Fork me on GitHub

seaborn绘制箱型图

基于Seaborn绘制箱型图

本文介绍的如何使用 seaborn 的 boxplot 方法来绘制箱型图。

参数

主要的参数如下:

官网地址:https://seaborn.pydata.org/generated/seaborn.boxplot.html

箱型图

箱型图是一种用作显示一组数据分散情况资料的统计图,它能够快速显示数据中的异常值情况,其形状像盒子,因而得名,也称之为盒须图、盒式图、盒装图或者箱型图。

1977年,美国著名数学家John W. Tukey首先在他的著作《Exploratory Data Analysis》中介绍了箱形图。

四分位数是箱型图中最为重要的概念。Q3和Q1的差距称为四分位距(InterQuartile Range, IQR):IQR=Q3-Q1

内置数据

Seaborn也有自己内置的数据集:

1
2
3
import seaborn as sns
# style设置
sns.set_theme(style="whitegrid")

tips

消费数据集tips

iris

知名的鸢尾花数据集

水平箱型图

In [4]:

1
2
3
# 方式1:指定x为某个Series型数据

ax = sns.boxplot(x=tips["total_bill"])
1
2
3
# 方式2:传入x和data参数
ax = sns.boxplot(x="total_bill",
data=tips)

垂直箱型图

In [6]:

1
2
3
4
ax = sns.boxplot(y=tips["total_bill"])

# 方式2:传入y和data参数
# ax = sns.boxplot(y="total_bill", data=tips)

参数orient

In [7]:

1
ax = sns.boxplot(x="day",y="total_bill", data=tips)

改变x-y的位置:

1
ax = sns.boxplot(y="day",x="total_bill", data=tips)

参数order

对指定的参数进行排序

In [11]:

1
2
3
4
5
6
# 默认情况
ax = sns.boxplot(
x="sex",
y="tip",
data=tips
)

下面的例子中我们引入了参数order,主要是查看x轴中两个标签;

In [12]:

1
2
3
4
5
6
ax = sns.boxplot(
x="sex",
y="tip",
data=tips,
order=["Female","Male"] # 引入参数
)

和默认情况下的排序不同,按照指定的顺序进行展示:

参数hue使用

参数hue主要是用来进行色条的调节

In [13]:

1
2
3
4
5
6
ax = sns.boxplot(
x="day",
y="tip",
data=tips,
hue="sex" # 引入参数
)

参数hue_order

In [14]:

1
2
3
4
5
6
7
ax = sns.boxplot(
x="day",
y="tip",
data=tips,
hue="sex",
hue_order=["Female","Male"] # 引入参数
)

参数palette

颜色版的设置使用palette

In [15]:

1
2
3
4
5
6
7
ax = sns.boxplot(
x="day",
y="tip",
data=tips,
hue="sex",
palette="Set3" # 颜色版
)

1
2
3
4
5
6
7
ax = sns.boxplot(
x="day",
y="tip",
data=tips,
hue="sex",
palette="Set2" # 颜色版
)

大小参数

主要是saturation、width、fliersize、linewidth、whis的设置

In [19]:

1
2
# 全部是默认情况
ax = sns.boxplot(x="sex",y="tip", data=tips, hue="day")

1
2
3
4
5
6
7
8
ax = sns.boxplot(
x="sex",
y="tip",
data=tips,
hue="day",
width=0.7,
linewidth=3,
)

1
2
3
4
5
6
7
8
9
ax = sns.boxplot(
x="sex",
y="tip",
data=tips,
hue="day",
width=0.7,
linewidth=3,
whis=3 # 引入whis
)

参数notch

自定义缺口

In [22]:

1
2
3
4
5
6
7
ax = sns.boxplot(
x="day",
y="total_bill",
hue="sex",
data=tips,
notch=True # 加入参数
)

参数dodge

必须和hue一起使用,控制同一个分组下面的箱型图是分开绘制还是重叠在一起

In [23]:

1
2
3
4
5
6
ax = sns.boxplot(
x="day",
y="total_bill",
hue="sex",
data=tips,
dodge=False)

1
2
3
4
5
6
ax = sns.boxplot(
x="day",
y="total_bill",
hue="sex",
data=tips,
dodge=True)

catplot-分类图

箱型图和分类图的结合使用

In [26]:

1
2
3
4
5
6
7
8
9
ax = sns.catplot(
x="sex",
y="total_bill",
hue="smoker",
col="time",
data=tips,
kind="box", # 箱型图
height=4,
aspect=.7)

1
2
3
4
5
6
7
8
9
10
11
12
ax = sns.catplot(
x="total_bill",
y="sex",
hue="smoker",
col="time",
data=tips,
orient="h", # 水平方向
kind="box", # 箱型图
height=4,
aspect=.7,
palette="Set2"
)

1
2
3
4
5
6
7
8
9
ax = sns.catplot(
x="sex",
y="total_bill",
hue="smoker",
col="time",
data=tips,
kind="violin", # 小提琴图
height=4,
aspect=.7)

本文标题:seaborn绘制箱型图

发布时间:2022年04月12日 - 22:04

原始链接:http://www.renpeter.cn/2022/04/12/seaborn%E7%BB%98%E5%88%B6%E7%AE%B1%E5%9E%8B%E5%9B%BE.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea