Public account: You and the cabin by: Peter Editor: Peter

Hello, I’m Peter

This article describes how to use Seaborn’s Boxplot method to draw box plots. Let’s take a look at some of the plots:

parameter

The main parameters in the drawing are as follows:

More information can refer to the website address: seaborn.pydata.org/generated/s…

Box figure

Box graph is a statistical graph used to display a group of data dispersion data, it can quickly display the outliers in the data, its shape like a box, hence the name, also known as box and beard graph, box graph, box graph or box graph.

In 1977, John W. Tukey, a famous American mathematician, first introduced box graphs in his book Exploratory Data Analysis.

Quartiles are the most important concept in box plots. The difference between Q3 and Q1 is called the quartile distance (InterQuartile Range, IQR) : IQR=Q3-Q1

Built-in data

Seaborn also has its own built-in data set:

import seaborn as sns
# style Settings
sns.set_theme(style="whitegrid")  
Copy the code

tips

Consumption data set TIPS

iris

The well-known iris data set

Horizontal box diagram

In [4]:

# method 1: Specify x as a Series data

ax = sns.boxplot(x=tips["total_bill"])
Copy the code
# Method 2: Pass in the x and data arguments
ax = sns.boxplot(x="total_bill",
                data=tips)
Copy the code

Vertical box diagram

In [6]:

# ax = sns.boxplot(y="total_bill", data=tips) # ax = sns.boxplot(y="total_bill", data=tips)Copy the code

Parameters of the received

In [7]:

ax = sns.boxplot(x="day",y="total_bill", data=tips)
Copy the code

Change the position of x-y:

ax = sns.boxplot(y="day",x="total_bill", data=tips)
Copy the code

Parameters of the order

Sorts the specified parameters

In [11]:

Ax = sns.boxplot(x="sex", y="tip", data=tips)Copy the code

In the following example, we introduce the parameter Order to look at two labels in the X-axis;

In [12]:

Ax = sns.boxplot(x="sex", y="tip", data=tips, order=["Female","Male"] #Copy the code

Instead of sorting by default, display in the order specified:

Hue Use

Hue is used to adjust the color bar

In [13]:

Ax = sns.boxplot(x="day", y="tip", data=tips, hue="sex")Copy the code

Parameter hue_order

In [14]:

ax = sns.boxplot(
    x="day",
    y="tip", 
    data=tips,
    hue="sex",
    hue_order=["Female"."Male"]  # introduce parameters
)
Copy the code

Parameters of the palette

The Settings for the color palette will be used in the Palette

In [15]:

Ax = sns.boxplot(x="day", y="tip", data=tips, Hue ="sex", Palette ="Set3" # color)Copy the code

ax = sns.boxplot(
    x="day",
    y="tip", 
    data=tips,
    hue="sex",
    palette="Set2"  # color version
)
Copy the code

Size parameters

They are mainly the sets of Saturation, Width, Fliersize, linewidth and whis

In [19]:

Ax = sns.boxplot(x="sex",y="tip", data=tips, hue="day")Copy the code

ax = sns.boxplot(
    x="sex",
    y="tip", 
    data=tips,
    hue="day",
    width=0.7,
    linewidth=3.)Copy the code

ax = sns.boxplot(
    x="sex",
    y="tip", 
    data=tips,
    hue="day",
    width=0.7,
    linewidth=3,
    whis=3  # introduction whis
)
Copy the code

Parameters of the notch

Custom notch

In [22]:

Ax = sns.boxplot(x="day", y="total_bill", hue="sex", data=tips, notch=True)Copy the code

Parameters of the dodge

Use this command with Hue to control whether the box diagrams 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)
Copy the code

ax = sns.boxplot(
    x="day", 
    y="total_bill",
    hue="sex",
    data=tips, 
    dodge=True)
Copy the code

Catplot – classification figure

Combination of box diagram and classification diagram

In [26]:

Smoker (x="sex", Y ="total_bill", hue="smoker", col="time", data=tips, kind="box", # smoker height=4, aspect=.7)Copy the code

ax = sns.catplot(
    x="total_bill",
    y="sex",            
    hue="smoker", 
    col="time",
    data=tips, 
    orient="h".# Horizontal direction
    kind="box".# box figure
    height=4, 
    aspect=7.,
    palette="Set2"
)
Copy the code

ax = sns.catplot(
    x="sex", 
    y="total_bill",
    hue="smoker", 
    col="time",
    data=tips, 
    kind="violin".# Violin diagram
    height=4, 
    aspect=7.)
Copy the code