📜  在Python中使用 Seaborn 的箱线图

📅  最后修改于: 2022-05-13 01:54:52.555000             🧑  作者: Mango

在Python中使用 Seaborn 的箱线图

Seaborn 是一个惊人的Python统计图形绘图可视化库。它提供了漂亮的默认样式和调色板,使统计图更具吸引力。它建立在 matplotlib 库之上,并紧密集成到 pandas 的数据结构中。

箱形图

箱线图有助于保持定量数据的分布,以便于变量之间或分类变量的跨级别的比较。箱线图的主体显示四分位数和中位数的置信区间(如果启用)。中位数在每个盒子的中位数都有水平线,而胡须的垂直线延伸到最极端,非异常数据点和大写字母是胡须末端的水平线。

示例 1:使用 violinplot() 对“fmri”数据集进行基本可视化

Python3
import seaborn
 
 
seaborn.set(style='whitegrid')
fmri = seaborn.load_dataset("fmri")
 
seaborn.boxplot(x="timepoint",
                y="signal",
                data=fmri)


Python3
import seaborn
 
 
seaborn.set(style='whitegrid')
tip = seaborn.load_dataset('tips')
 
seaborn.boxplot(x='day', y='tip', data=tip)


Python3
# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("tips")
 
seaborn.boxplot(x =tip['total_bill'])


Python3
# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("tips")
seaborn.boxplot(x ='tip', y ='day', data = tip)


Python3
# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("fmri")
 
seaborn.boxplot(x ="timepoint",
             y ="signal",
             hue ="region",
             data = fmri)


Python3
# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("tips")
seaborn.boxplot(x = 'day', y = 'tip',
                data = tip,
                linewidth=2.5)


Python3
# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("tips")
seaborn.boxplot(x="day", y="total_bill",
                hue="smoker",
                data=tips, palette="Set2",
                dodge=True)


Python3
# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("tips")
seaborn.boxplot(data = tip,orient="h")


Python3
seaborn.boxplot(data = tip,orient="v")


Python3
# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("tips")
seaborn.boxplot(x = 'day', y = 'tip', data = tip,color = "green")


输出:

示例 2:使用 boxplot() 对“tips”数据集进行基本可视化

Python3

import seaborn
 
 
seaborn.set(style='whitegrid')
tip = seaborn.load_dataset('tips')
 
seaborn.boxplot(x='day', y='tip', data=tip)

输出:

Seaborn箱线图中具有不同属性的变量分组

1. 仅使用一个轴绘制单个水平箱线图:

如果我们只使用一个数据变量而不是两个数据变量,那么这意味着轴将这些数据变量中的每一个都表示为一个轴。

X表示x轴,y表示y轴。

句法:

seaborn.boxplot(x)

Python3

# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("tips")
 
seaborn.boxplot(x =tip['total_bill'])

输出:

2.绘制水平箱线图:

在上面的示例中,我们看到了如何绘制单个水平箱线图,在这里可以执行多个水平箱线图,并与另一个轴交换数据变量。

Python3

# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("tips")
seaborn.boxplot(x ='tip', y ='day', data = tip)

输出:

3.使用色调参数:

虽然点是在二维中绘制的,但可以通过根据第三个变量对点着色来将另一个维度添加到图中。

句法:

Python3

# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("fmri")
 
seaborn.boxplot(x ="timepoint",
             y ="signal",
             hue ="region",
             data = fmri)

输出:

4. 使用线宽在数据点周围绘制轮廓:

构成绘图元素的灰线的宽度。每当我们增加线宽时,点也会自动增加。

句法:

Python3

# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("tips")
seaborn.boxplot(x = 'day', y = 'tip',
                data = tip,
                linewidth=2.5)

输出:

5. 在主分类轴上的不同位置绘制色调变量的每个级别:

使用色调嵌套时,设置 dodge 应为 True 将沿分类轴分隔不同色调级别的点。而Palette用于不同层次的hue变量。

句法:

Python3

# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("tips")
seaborn.boxplot(x="day", y="total_bill",
                hue="smoker",
                data=tips, palette="Set2",
                dodge=True)

输出:

6. 控制绘图方向(垂直或水平)

当我们使用 orient 作为“h”时,它会绘制垂直线,如果我们使用“V”,那么指的是垂直线。

句法:

Python3

# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("tips")
seaborn.boxplot(data = tip,orient="h")

输出:

让我们检查垂直方向:

Python3

seaborn.boxplot(data = tip,orient="v")

输出:

7.对所有元素使用颜色的颜色属性:

Python3

# Python program to illustrate
# boxplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import seaborn
 
# use to set style of background of plot
seaborn.set(style="whitegrid")
 
# loading data-set
tip = seaborn.load_dataset("tips")
seaborn.boxplot(x = 'day', y = 'tip', data = tip,color = "green")

输出: