在Python中使用 Seaborn 的箱线图
Seaborn 是一个惊人的Python统计图形绘图可视化库。它提供了漂亮的默认样式和调色板,使统计图更具吸引力。它建立在 matplotlib 库之上,并紧密集成到 pandas 的数据结构中。
箱形图
箱线图有助于保持定量数据的分布,以便于变量之间或分类变量的跨级别的比较。箱线图的主体显示四分位数和中位数的置信区间(如果启用)。中位数在每个盒子的中位数都有水平线,而胡须的垂直线延伸到最极端,非异常数据点和大写字母是胡须末端的水平线。
Syntax: seaborn.boxplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, width=0.8, dodge=True, fliersize=5, linewidth=None, whis=1.5, ax=None, **kwargs)
Parameters:
x, y, hue: Inputs for plotting long-form data.
data: Dataset for plotting. If x and y are absent, this is interpreted as wide-form.
color: Color for all of the elements.
Returns: It returns the Axes object with the plot drawn onto it.
示例 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.使用色调参数:
虽然点是在二维中绘制的,但可以通过根据第三个变量对点着色来将另一个维度添加到图中。
句法:
seaborn.boxplot(x, y, hue, data);
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. 使用线宽在数据点周围绘制轮廓:
构成绘图元素的灰线的宽度。每当我们增加线宽时,点也会自动增加。
句法:
seaborn.boxplot(x, y, data, linewidth)
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变量。
句法:
seaborn.boxplot(x, y, data, hue, palette, dodge)
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)
输出:
Possible values of palette are:
Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r,
GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r,
Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r,
Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1,
Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr,
YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r,
cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth,
gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern,
6. 控制绘图方向(垂直或水平) :
当我们使用 orient 作为“h”时,它会绘制垂直线,如果我们使用“V”,那么它指的是垂直线。
句法:
seaborn.boxplot( data, orient )
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")
输出: