Public number: You Er Hut
Author: Peter
Editor: Peter
Hello everyone, my name is Peter~
This article describes how to use seaborn's boxplot method to draw box plots. Let's first look at the drawing effect of some graphics:
parameter
The main parameters in drawing the graph are as follows:
For more information, please refer to the official website address: seaborn.pydata.org/generated/s…
boxplot
A box chart is a statistical chart used to display the dispersion of a set of data. It can quickly display outliers in the data. It is shaped like a box, hence the name. It is also called a box and whisker chart. , box plot or box plot .
In 1977, the famous American mathematician John W. Tukey first introduced the box plot in his book "Exploratory Data Analysis".
Quartiles are the most important concept in box plots. The gap between Q3 and Q1 is called InterQuartile Range (IQR) : IQR=Q3-Q1
Built-in data
Seaborn also has its own built-in dataset:
import seaborn as sns
# style设置
sns.set_theme(style="whitegrid")
复制代码
tips
Consumption dataset tips
iris
Well-known iris dataset
Horizontal Box Plot
In [4]:
# 方式1:指定x为某个Series型数据
ax = sns.boxplot(x=tips["total_bill"])
复制代码
# 方式2:传入x和data参数
ax = sns.boxplot(x="total_bill",
data=tips)
复制代码
Vertical Box Plot
In [6]:
ax = sns.boxplot(y=tips["total_bill"])
# 方式2:传入y和data参数
# ax = sns.boxplot(y="total_bill", data=tips)
复制代码
parameter orient
In [7]:
ax = sns.boxplot(x="day",y="total_bill", data=tips)
复制代码
Change the position of xy:
ax = sns.boxplot(y="day",x="total_bill", data=tips)
复制代码
parameter order
Sorts the specified parameter
In [11]:
# 默认情况
ax = sns.boxplot(
x="sex",
y="tip",
data=tips
)
复制代码
In the following example, we introduce the parameter order, mainly to view the two labels in the x-axis;
In [12]:
ax = sns.boxplot(
x="sex",
y="tip",
data=tips,
order=["Female","Male"] # 引入参数
)
复制代码
Unlike the default sorting, it is displayed in the specified order:
Parameter hue use
The parameter hue is mainly used to adjust the color bar
In [13]:
ax = sns.boxplot(
x="day",
y="tip",
data=tips,
hue="sex" # 引入参数
)
复制代码
Parameters hue_order
In [14]:
ax = sns.boxplot(
x="day",
y="tip",
data=tips,
hue="sex",
hue_order=["Female","Male"] # 引入参数
)
复制代码
Parameters palette
Use the palette to set the color version
In [15]:
ax = sns.boxplot(
x="day",
y="tip",
data=tips,
hue="sex",
palette="Set3" # 颜色版
)
复制代码
ax = sns.boxplot(
x="day",
y="tip",
data=tips,
hue="sex",
palette="Set2" # 颜色版
)
复制代码
size parameter
Mainly the settings of saturation, width, fliersize, linewidth, whis
In [19]:
# 全部是默认情况
ax = sns.boxplot(x="sex",y="tip", data=tips, hue="day")
复制代码
ax = sns.boxplot(
x="sex",
y="tip",
data=tips,
hue="day",
width=0.7,
linewidth=3,
)
复制代码
ax = sns.boxplot(
x="sex",
y="tip",
data=tips,
hue="day",
width=0.7,
linewidth=3,
whis=3 # 引入whis
)
复制代码
parameter notch
custom notch
In [22]:
ax = sns.boxplot(
x="day",
y="total_bill",
hue="sex",
data=tips,
notch=True # 加入参数
)
复制代码
parameter dodge
Must be used with hue to control whether the boxplots under the same group are drawn separately or overlapped
In [23]:
ax = sns.boxplot(
x="day",
y="total_bill",
hue="sex",
data=tips,
dodge=False)
复制代码
ax = sns.boxplot(
x="day",
y="total_bill",
hue="sex",
data=tips,
dodge=True)
复制代码
catplot - classification plot
Combining Box Plots and Classification Plots
In [26]:
ax = sns.catplot(
x="sex",
y="total_bill",
hue="smoker",
col="time",
data=tips,
kind="box", # 箱型图
height=4,
aspect=.7)
复制代码
ax = sns.catplot(
x="total_bill",
y="sex",
hue="smoker",
col="time",
data=tips,
orient="h", # 水平方向
kind="box", # 箱型图
height=4,
aspect=.7,
palette="Set2"
)
复制代码
ax = sns.catplot(
x="sex",
y="total_bill",
hue="smoker",
col="time",
data=tips,
kind="violin", # 小提琴图
height=4,
aspect=.7)
复制代码