boxplot函数--Matplotlib

boxplot函数–Matplotlib

函数功能:
Make a box and whisker plot.
绘制盒须图
Make a box and whisker plot for each column of x or each vector in sequence x. The box extends from the lower to upper quartile values of the data, with a line at the median. The whiskers extend from the box to show the range of the data. Flier points are those past the end of the whiskers.
为x的每一列或序列x中的每个向量绘制盒须图。盒子范围从数据的下四分位数到上四分位数,中间带有中位数的线。须从盒身延伸来展示数据的范围。离群点是那些在须外的点。
函数语法:
boxplot(x, notch=None, sym=None, vert=None, whis=None, positions=None, widths=None, patch_artist=None, bootstrap=None, usermedians=None, conf_intervals=None, meanline=None, showmeans=None, showcaps=None, showbox=None, showfliers=None, boxprops=None, labels=None, flierprops=None, medianprops=None, meanprops=None, capprops=None, whiskerprops=None, manage_ticks=True, autorange=False, zorder=None, *, data=None)
函数参数:
x: Array or a sequence of vectors. The input data.
x: 输入数据,数组或者向量序列。

import matplotlib.pyplot as plt

x = [6, 47, 49, 15, 42, 41, 7, 39, 12,
     21, 22, 34, 34, 25, 27, 28, 30,
     22, 31, 32, 33, 34, 35, 36, 21,
     6, 47, 49, 15, 42, 41, 7, 39, 11,
     43, 40, 36, 20, 33, -40, 100]

plt.boxplot(x)

plt.show()

在这里插入图片描述

notch: bool, default: False
Whether to draw a notched box plot (True), or a rectangular box plot (False). The notches represent the confidence interval (CI) around the median. The documentation for bootstrap describes how the locations of the notches are computed.
盒须图形状:布尔型,默认值:FALSE。
是否绘制缺口箱图,当参数 n o t c h notch notch为True时,绘制缺口箱图,当参数 n o t c h notch notch为False时,绘制矩形箱图。缺口代表中位数的置信区间(CI)。 b o o t s t r a p bootstrap bootstrap文档描述了缺口位置的计算方式。


import matplotlib.pyplot as plt

x = [6, 47, 49, 15, 42, 41, 7, 39, 12,
     21, 22, 34, 34, 25, 27, 28, 30,
     22, 31, 32, 33, 34, 35, 36, 21,
     6, 47, 49, 15, 42, 41, 7, 39, 11,
     43, 40, 36, 20, 33, -40, 100]

plt.boxplot(x, notch=True)

plt.show()

在这里插入图片描述

sym: str, optional
The default symbol for flier points. An empty string (’’) hides the fliers. If None, then the fliers default to ‘b+’. More control is provided by the flierprops parameter.
离群点的表示符号:字符串类型,可选参数,可同时设置颜色和符号。
离群点的默认样式,空字符串 ′ ′ '' ,表示隐藏离群值,离群值被忽略。若为None,则
离群点默认为“b+”,即蓝色的 + + +号(默认明明为黑色的空心圆圈啊,见上图???)。更多控制通过参数 f l i e r p r o p s flierprops flierprops提供。

  1. 更改参数sym=’^’,改变离群值的标记样式

import matplotlib.pyplot as plt

x = [6, 47, 49, 15, 42, 41, 7, 39, 12,
     21, 22, 34, 34, 25, 27, 28, 30,
     22, 31, 32, 33, 34, 35, 36, 21,
     6, 47, 49, 15, 42, 41, 7, 39, 11,
     43, 40, 36, 20, 33, -40, 100]

plt.boxplot(x, notch=True, sym='^')

plt.show()

在这里插入图片描述

  1. 当参数 s y m = ′ ′ sym='' sym=, 离群值被忽略,不再绘制。

import matplotlib.pyplot as plt

x = [6, 47, 49, 15, 42, 41, 7, 39, 12,
     21, 22, 34, 34, 25, 27, 28, 30,
     22, 31, 32, 33, 34, 35, 36, 21,
     6, 47, 49, 15, 42, 41, 7, 39, 11,
     43, 40, 36, 20, 33, -40, 100]

plt.boxplot(x, notch=True, sym='')

plt.show()

在这里插入图片描述
3.同时设置离群值标记的颜色和样式

import matplotlib.pyplot as plt

x = [6, 47, 49, 15, 42, 41, 7, 39, 12,
     21, 22, 34, 34, 25, 27, 28, 30,
     22, 31, 32, 33, 34, 35, 36, 21,
     6, 47, 49, 15, 42, 41, 7, 39, 11,
     43, 40, 36, 20, 33, -40, 100]

plt.boxplot(x, notch=True, sym='r*')

plt.show()

在这里插入图片描述
离群值的点显示除了可以通过参数 sym=’’ 来实现,还可以通过参数 showfliers实现

showfliers: bool, default: True
Show the outliers beyond the caps.
是否显示异常值: 布尔型,默认值为:True
显示须线末尾水平线外的异常值
1.当参数 showfliers=False,参数 sym 不能发挥作用

import matplotlib.pyplot as plt

x = [6, 47, 49, 15, 42, 41, 7, 39, 12,
     21, 22, 34, 34, 25, 27, 28, 30,
     22, 31, 32, 33, 34, 35, 36, 21,
     6, 47, 49, 15, 42, 41, 7, 39, 11,
     43, 40, 36, 20, 33, -40, 100]

plt.boxplot(x, notch=True, sym='r*',
            showfliers=False)

plt.show()

在这里插入图片描述

2.当参数 showfliers=True,参数 sym=’’ ,离群点不能显示

import matplotlib.pyplot as plt

x = [6, 47, 49, 15, 42, 41, 7, 39, 12,
     21, 22, 34, 34, 25, 27, 28, 30,
     22, 31, 32, 33, 34, 35, 36, 21,
     6, 47, 49, 15, 42, 41, 7, 39, 11,
     43, 40, 36, 20, 33, -40, 100]

plt.boxplot(x, notch=True, sym='',
            showfliers=True)

plt.show()

在这里插入图片描述

只有当参数 showfliers=True且参数 sym不为空 ,离群点才能正常显示


import matplotlib.pyplot as plt

x = [6, 47, 49, 15, 42, 41, 7, 39, 12,
     21, 22, 34, 34, 25, 27, 28, 30,
     22, 31, 32, 33, 34, 35, 36, 21,
     6, 47, 49, 15, 42, 41, 7, 39, 11,
     43, 40, 36, 20, 33, -40, 100]

plt.boxplot(x, notch=True, sym='b+',
            showfliers=True)

plt.show()

在这里插入图片描述

vert: bool, default: True
If True, draws vertical boxes. If False, draw horizontal boxes.
盒须图是否垂直绘制:布尔型,默认值:True
若为True,绘制垂直的盒须图;若为False,绘制水平的盒须图。

默认值为True

import matplotlib.pyplot as plt

x = [6, 47, 49, 15, 42, 41, 7, 39, 12,
     21, 22, 34, 34, 25, 27, 28, 30,
     22, 31, 32, 33, 34, 35, 36, 21,
     6, 47, 49, 15, 42, 41, 7, 39, 11,
     43, 40, 36, 20, 33, -40, 100]

plt.boxplot(x, notch=True, sym='r*',
            vert=True)

plt.show()

在这里插入图片描述

当参数 v e r t = F a l s e vert=False vert=False,绘制水平方向柱形图

import matplotlib.pyplot as plt

x = [6, 47, 49, 15, 42, 41, 7, 39, 12,
     21, 22, 34, 34, 25, 27, 28, 30,
     22, 31, 32, 33, 34, 35, 36, 21,
     6, 47, 49, 15, 42, 41, 7, 39, 11,
     43, 40, 36, 20, 33, -40, 100]

plt.boxplot(x, notch=True, sym='r*',
            vert=False)

plt.show()

在这里插入图片描述

whis: float or (float, float), default: 1.5
The position of the whiskers.
须的位置:单个浮点数或一对浮点型坐标,默认值:单个浮点数字1.5
If a float, the lower whisker is at the lowest datum above Q1 - whis*(Q3-Q1), and the upper whisker at the highest datum below Q3 + whis*(Q3-Q1), where Q1 and Q3 are the first and third quartiles. The default value of whis = 1.5 corresponds to Tukey’s original definition of boxplots.
若为单个浮点型数字,下面的须在 Q 1 − w h i s ∗ ( Q 3 − Q 1 ) Q1 - whis*(Q3-Q1) Q1whis(Q3Q1)的基线上的最低位置,上面的须在 Q 3 + w h i s ∗ ( Q 3 − Q 1 ) Q3 + whis*(Q3-Q1) Q3+whis(Q3Q1)线下的最高位置, Q 1 , Q 3 Q1,Q3 Q1,Q3是第一、第三分位数。默认值 w h i s = 1.5 whis=1.5 whis=1.5与约翰·图基(John Tukey,统计学家,盒须图的发明者)原本的定义一致。
If a pair of floats, they indicate the percentiles at which to draw the whiskers (e.g., (5, 95)). In particular, setting this to (0, 100) results in whiskers covering the whole range of the data. “range” is a deprecated synonym for (0, 100).
若参数 w h i s whis whis为一对浮点型数字坐标,这对坐标表明绘制须的百分位数(下面的不理解,实际展示效果也没看明白
In the edge case where Q1 == Q3, whis is automatically set to (0, 100) (cover the whole range of the data) if autorange is True.

Beyond the whiskers, data are considered outliers and are plotted as individual points.
须线之外的数据认为是异常值,将单独绘制成点。

参数 w h i s = 1.5 whis=1.5 whis=1.5,为单个浮点型数字

import matplotlib.pyplot as plt

x = [6, 47, 49, 15, 42, 41, 7, 39, 12,
     21, 22, 34, 34, 25, 27, 28, 30,
     22, 31, 32, 33, 34, 35, 36, 21,
     6, 47, 49, 15, 42, 41, 7, 39, 11,
     43, 40, 36, 20, 33, -40, 100]

plt.boxplot(x, notch=True, sym='b+',
            whis=1.5)

plt.show()

在这里插入图片描述

随着参数 w h i s whis whis的增大,须线不会任意相应延长,当须线的数据范围包括整个数据值范围时,须线不再延伸。
参数 w h i s = 500 whis=500 whis=500,须线并不会增大到指定的计算数字。

import matplotlib.pyplot as plt

x = [6, 47, 49, 15, 42, 41, 7, 39, 12,
     21, 22, 34, 34, 25, 27, 28, 30,
     22, 31, 32, 33, 34, 35, 36, 21,
     6, 47, 49, 15, 42, 41, 7, 39, 11,
     43, 40, 36, 20, 33, -40, 100]

plt.boxplot(x, notch=True, sym='b+',
            whis=500)

plt.show()

在这里插入图片描述

bootstrap: int, optional
Specifies whether to bootstrap the confidence intervals around the median for notched boxplots. If bootstrap is None, no bootstrapping is performed, and notches are calculated using a Gaussian-based asymptotic approximation (see McGill, R., Tukey, J.W., and Larsen, W.A., 1978, and Kendall and Stuart, 1967). Otherwise, bootstrap specifies the number of times to bootstrap the median to determine its 95% confidence intervals. Values between 1000 and 10000 are recommended.
bootstarp: 整数,可选参数
指定绘制凹槽盒须图时是否使用bootstarp方法估计中位数的置信区间。若 b o o t s t r a p = N o n e bootstrap=None bootstrap=None(默认),bootstrap方法不执行,凹槽基于高斯的渐进估计值计算。否则,参数 b o o t s t r a p bootstrap bootstrap指定中位数自展的次数以估计95%的置信区间。推荐使用1000-10000之间的数值。

当参数 b o o t s t a p = N o n e bootstap=None bootstap=None

import matplotlib.pyplot as plt

x = [6, 47, 49, 15, 42, 41, 7, 39, 12,
     21, 22, 34, 34, 25, 27, 28, 30,
     22, 31, 32, 33, 34, 35, 36, 21,
     6, 47, 49, 15, 42, 41, 7, 39, 11,
     43, 40, 36, 20, 33, -40, 100]

plt.boxplot(x, notch=True, sym='b+',
            whis=1.5, bootstrap=None)

plt.show()

在这里插入图片描述

当参数 b o o t s t a p = 2000 bootstap=2000 bootstap=2000

import matplotlib.pyplot as plt

x = [6, 47, 49, 15, 42, 41, 7, 39, 12,
     21, 22, 34, 34, 25, 27, 28, 30,
     22, 31, 32, 33, 34, 35, 36, 21,
     6, 47, 49, 15, 42, 41, 7, 39, 11,
     43, 40, 36, 20, 33, -40, 100]

plt.boxplot(x, notch=True, sym='b+',
            whis=1.5, bootstrap=2000)

plt.show()

在这里插入图片描述

注:Bootstrap方法是非常有用的一种统计学上的估计方法,是斯坦福统计系的教授Bradley Efron)在总结、归纳前人研究成果的基础上提出一种新的非参数统计方法。Bootstrap是一类非参数Monte Carlo方法,其实质是对观测信息进行再抽样,进而对总体的分布特性进行统计推断。
因为该方法充分利用了给定的观测信息,不需要模型其他的假设和增加新的观测,并且具有稳健性和效率高的特点。1980年代以来,随着计算机技术被引入到统计实践中来,此方法越来越受欢迎,在机器学习领域应用也很广泛。
Bootstrap步骤:

  1. 在原有的样本中通过重抽样抽取一定数量(比如100)的新样本,重抽样(Re-sample)的意思就是有放回的抽取,即一个数据有可以被重复抽取超过一次。
  2. 基于产生的新样本,计算我们需要估计的统计量。

本部分参考Bootstrap
*
usermedians: array-like, optional
A 1D array-like of length len(x). Each entry that is not None forces the value of the median for the corresponding dataset. For entries that are None, the medians are computed by Matplotlib as normal.
可选参数,数组。(不理解

conf_intervals: array-like, optional
A 2D array-like of shape (len(x), 2). Each entry that is not None forces the location of the corresponding notch (which is only drawn if notch is True). For entries that are None, the notches are computed by the method specified by the other parameters (e.g., bootstrap)
可选参数,数组。(不理解

positions: array-like, optional
Sets the positions of the boxes. The ticks and limits are automatically set to match the positions. Defaults to range(1, N+1) where N is the number of boxes to be drawn.
位置,可选参数,数组。
设置箱线图中箱子的位置,刻度和最值会自动设置以匹配箱子位置。默认范围为range(1,N+1)函数,即:1,2,…,N,N为需要绘制的箱子数量

  1. 当只有一个盒须图,默认在1的位置
import matplotlib.pyplot as plt

x = [6, 47, 49, 15, 42, 41, 7, 39, 12,
     21, 22, 34, 34, 25, 27, 28, 30,
     22, 31, 32, 33, 34, 35, 36, 21,
     6, 47, 49, 15, 42, 41, 7, 39, 11,
     43, 40, 36, 20, 33, -40, 100]

plt.boxplot(x, notch=True, sym='b+',
            whis=1.5, bootstrap=2000)

plt.show()

在这里插入图片描述

  1. 指定盒须图位置时,需要使用数组的形式,即使只有一个盒须图
    参数 p o s i t i o n s = ( 2 , ) positions=(2,) positions=(2,)

import matplotlib.pyplot as plt

x = [6, 47, 49, 15, 42, 41, 7, 39, 12,
     21, 22, 34, 34, 25, 27, 28, 30,
     22, 31, 32, 33, 34, 35, 36, 21,
     6, 47, 49, 15, 42, 41, 7, 39, 11,
     43, 40, 36, 20, 33, -40, 100]

plt.boxplot(x, notch=True, sym='b+',
            whis=1.5, bootstrap=2000,
            positions=(2,))

plt.show()

在这里插入图片描述

  1. 多个盒须图指定位置,如:设置两个盒须图输出在(2,3)位置,使用参数 p o s i t i o n s = ( 2 , 3 ) positions=(2,3) positions=(2,3)为数组

import matplotlib.pyplot as plt
import numpy as np

x = [6, 47, 49, 15, 42, 41, 7, 39, 12,
     21, 22, 34, 34, 25, 27, 28, 30,
     22, 31, 32, 33, 34, 35, 36, 21,
     6, 47, 49, 15, 42, 41, 7, 39, 11,
     43, 40, 36, 20, 33, -40, 100]
y = np.random.randint(0, 60, 200)

plt.boxplot((x, y), notch=True, sym='b+',
            whis=1.5, bootstrap=2000,
            positions=(2, 3))

plt.show()

在这里插入图片描述

widths: float or array-like
Sets the width of each box either with a scalar or a sequence. The default is 0.5, or 0.15*(distance between extreme positions), if that is smaller.

箱体宽度:浮点型或者数组
使用标量或者列表设置每个箱体的宽度。默认宽度为 0.5(实践默认0.25) ,若小一点则为: 0.15 ∗ 极 限 位 置 之 间 距 离 0.15*极限位置之间距离 0.15

1) 默认宽度

import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(200)
y = np.random.randn(200)
a = np.linspace(0, 1, 5)
plt.boxplot(x, sym='r*', notch=True, whis=1.5,
            positions=(.5,))
plt.xticks(ticks=a, labels=a)
plt.show()

在这里插入图片描述
2) 指定宽度:
箱体位置在 p o s i t i o n s s = 1.5 positionss=1.5 positionss=1.5,宽度设置 w i d t h s = 1 widths=1 widths=1

import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(200)
y = np.random.randn(200)
a = np.linspace(0, 3, 7)
plt.boxplot(x, sym='r*', notch=True, whis=1.5,
            positions=(1.5,), widths=1)
plt.xticks(ticks=a, labels=a)
plt.show()

在这里插入图片描述

3)多个箱体存在时默认宽度

当有多个盒须图时,箱体的宽度为0.15*(箱体横坐标最大差值)

当图中有两个箱体,且箱体的横坐标没有改变时,箱体的宽度为 0.15 ∗ ( 2 − 1 ) = 0.15 0.15*(2-1)=0.15 0.15(21)=0.15

import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(200)
y = np.random.randn(200)
a = np.linspace(0, 3, 13)
b = list(np.linspace(0, 3, 13))
plt.boxplot((x, y), sym='r*', notch=True, whis=1.5)
plt.xticks(ticks=a, labels=b)
plt.xlim(0, 3)
plt.show()

在这里插入图片描述

当改变两个箱体横坐标值为 ( 0.5 , 2.5 ) (0.5,2.5) 0.5,2.5,箱体的宽度为 0.15 ∗ ( 2.5 − 0.5 ) = 0.3 0.15*(2.5-0.5)=0.3 0.15(2.50.5)=0.3

import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(200)
y = np.random.randn(200)
a = np.linspace(0, 3, 13)
b = list(np.linspace(0, 3, 13))
plt.boxplot((x, y), sym='r*', notch=True, whis=1.5, positions=(0.5, 2.5))
plt.xticks(ticks=a, labels=b)
plt.xlim(0, 3)
plt.show()

在这里插入图片描述
patch_artist: bool, default: False
If False produces boxes with the Line2D artist. Otherwise, boxes and drawn with Patch artists.
是否填充箱体颜色: 布尔值,默认值为:False;实践证明当该参数为False,则箱体无颜色,当参数为True,箱体颜色填充。
当参数patch_ artist=True

import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(200)
y = np.random.randn(200)
a = np.linspace(0, 3, 13)
b = list(np.linspace(0, 3, 13))
plt.boxplot((x, y), sym='r*', notch=True, whis=1.5,
            patch_artist=True)
plt.xticks(ticks=a, labels=b)
plt.xlim(0, 3)
plt.show()

在这里插入图片描述
labels: sequence, optional
Labels for each dataset (one per dataset).
标签: 列表,可选参数
每个数据集(单个数据集)的标签。

在对盒须图进行标签的过程中,x轴标签不能再进行输出,否则会覆盖盒须图标签。此标签非之前的图例中的文字内容标签,而是盒须图下面x轴内容对应的标签。

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
x = np.random.randn(200)
y = np.random.randn(200)
a = np.linspace(0, 3, 13)
b = list(np.linspace(0, 3, 13))
c = ['AAAA']
plt.boxplot(x,sym='r*', notch=True, whis=1.5,
            patch_artist=True, labels=c)
# plt.xticks(ticks=a, labels=b)
plt.xlim(0, 3)
# plt.legend(loc='lower left')
plt.show()

在这里插入图片描述
manage_ticks: bool, default: True
If True, the tick locations and labels will be adjusted to match the boxplot positions.
管理刻度:布尔型,默认值为True
若为True,则刻度的位置和标签会根据箱子的位置调整。

当参数manage_ticks=False,设置的labels标签内容不会显示

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
x = np.random.randn(200)
y = np.random.randn(200)
a = np.linspace(0, 3, 13)
b = list(np.linspace(0, 3, 13))
c = ['AAAA']
plt.boxplot(x, sym='r*', notch=True, whis=1.5, positions=(2,),
            patch_artist=True, labels=c, manage_ticks=False)
# plt.xticks(ticks=a, labels=b)
plt.xlim(0, 3)
# plt.legend(loc='lower left')
print(plt.xlim())
plt.show()

在这里插入图片描述
当参数manage_ticks=True,设置的labels标签内容会显示

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']=False
x = np.random.randn(200)
y = np.random.randn(200)
a = np.linspace(0, 3, 13)
b = list(np.linspace(0, 3, 13))
c = ['AAAA']
plt.boxplot(x, sym='r*', notch=True, whis=1.5, positions=(2,),
            patch_artist=True, labels=c, manage_ticks=True)
# plt.xticks(ticks=a, labels=b)
plt.xlim(0, 3)
# plt.legend(loc='lower left')
print(plt.xlim())
plt.show()

在这里插入图片描述
autorange: bool, default: False
When True and the data are distributed such that the 25th and 75th percentiles are equal, whis is set to (0, 100) such that the whisker ends are at the minimum and maximum of the data.
须线范围设置: 布尔值,默认值:False
当参数 a u t o r a n g e = T r u e autorange=True autorange=True 且数据的下四分位数与上四分位数相等,参数 w h i s whis whis设置为(0,100),以使须线的末端为数据的最大/最小值。

当参数autorange=False,且数据集的Q1=Q3,箱体与箱须均在一条线上

import matplotlib.pyplot as plt
import numpy as np

x = [-20, 1, 2, 3, 4, 7, 8, 12, 14, 10]
y = [-20, 1, 2, 3, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 8, 9, 10]
c = ['AAAA']
plt.boxplot(y, sym='r*', notch=False, whis=1.5, positions=(2,),
            patch_artist=True, labels=c, manage_ticks=True,
            autorange=False)
plt.show()

在这里插入图片描述

而当参数autorange=True,且数据集的Q1=Q3,虽然计算的箱须长度为0,但箱须会延伸至数据集的最大/最小值。

import matplotlib.pyplot as plt
import numpy as np

x = [-20, 1, 2, 3, 4, 7, 8, 12, 14, 10]
y = [-20, 1, 2, 3, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 8, 9, 10]
c = ['AAAA']
plt.boxplot(y, sym='r*', notch=False, whis=1.5, positions=(2,),
            patch_artist=True, labels=c, manage_ticks=True,
            autorange=True)
plt.show()

在这里插入图片描述
showmeans: bool, default: False
Show the arithmetic means.
显示数据集均值:布尔型,默认值:False
显示算数平均值
只有参数showmeans,则均值以点显示


import matplotlib.pyplot as plt
import numpy as np

x = [6, 47, 49, 15, 42, 41, 7, 39, 12,
     21, 22, 34, 34, 25, 27, 28, 30,
     22, 31, 32, 33, 34, 35, 36, 21,
     6, 47, 49, 15, 42, 41, 7, 39, 11,
     43, 40, 36, 20, 33, -40, 100]
y = np.random.randint(0, 60, 200)

plt.boxplot((x, y), notch=True, sym='b+',
            whis=1.5, bootstrap=2000,
            positions=(2, 3), showmeans=True)

plt.show()


在这里插入图片描述

meanline: bool, default: False
If True (and showmeans is True), will try to render the mean as a line spanning the full width of the box according to meanprops (see below). Not recommended if shownotches is also True. Otherwise, means will be shown as points.
是否以线的形式表示均值:布尔型,默认值:False
若参数meanline=True且参数showmeans=True,将根据meanprops将均值呈现为跨越整个箱体宽度的线。若参数shownotches同时为True,不推荐使用参数meanline。否则,均值将会以点的形式展示。(不理解,函数中没有shownotches**参数)

  1. 当参数showmeans=True且参数meanline=True,均值会显示且以线条显示

import matplotlib.pyplot as plt
import numpy as np

x = [6, 47, 49, 15, 42, 41, 7, 39, 12,
     21, 22, 34, 34, 25, 27, 28, 30,
     22, 31, 32, 33, 34, 35, 36, 21,
     6, 47, 49, 15, 42, 41, 7, 39, 11,
     43, 40, 36, 20, 33, -40, 100]
y = np.random.randint(0, 60, 200)

plt.boxplot((x, y), notch=True, sym='b+',
            whis=1.5, bootstrap=2000,
            positions=(2, 3), showmeans=True, meanline=True)

plt.show()

在这里插入图片描述

zorder: float, default: Line2D.zorder = 2
Sets the zorder of the boxplot.
(不理解)

showcaps: bool, default: True
Show the caps on the ends of whiskers.
是否显示箱线图顶端和末端的两条线:布尔型,默认值:True
显示箱线图顶端和末端的两条线

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

x = [-20, 1, 2, 3, 4, 7, 8, 12, 14, 10]
y = [-20, 1, 2, 3, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 8, 9, 10]
c = ['AAAA']
plt.boxplot(x, sym='r*', notch=True, whis=120, positions=(2,),
            patch_artist=True, labels=c, manage_ticks=True,
            autorange=False, meanline=True, showmeans=True,
            showcaps=False)
plt.show()

在这里插入图片描述
showbox: bool, default: True
Show the central box

是否显示箱线图的箱体:布尔型,默认值:True
显示箱线图的箱体

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

x = [-20, 1, 2, 3, 4, 7, 8, 12, 14, 10]
y = [-20, 1, 2, 3, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 8, 9, 10]
c = ['AAAA']
plt.boxplot(x, sym='r*', notch=True, whis=120, positions=(2,),
            patch_artist=True, labels=c, manage_ticks=True,
            autorange=False, meanline=True, showmeans=True,
            showcaps=False, showbox=False)
plt.show()

在这里插入图片描述

capprops: dict, default: None
The style of the caps.

boxprops: dict, default: None
The style of the box.

参数linestyle没有起作用(???)

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

x = [-20, 1, 2, 3, 4, 7, 8, 12, 14, 10]
y = [-20, 1, 2, 3, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 8, 9, 10]
c = ['AAAA']
plt.boxplot(x, sym='', notch=False, whis=1.5, positions=(2,),
            patch_artist=True, labels=c, manage_ticks=True,
            autorange=True, showmeans=True, showfliers=True,
            boxprops={'edgecolor':'black', 'facecolor':'cyan','linestyle':':','linewidth':'4'})
plt.show()

在这里插入图片描述

whiskerprops: dict, default: None
The style of the whiskers.

flierprops: dict, default: None
The style of the fliers.

medianprops: dict, default: None
The style of the median.

meanprops: dict, default: None
The style of the mean.

这些每个参数里有哪些属性,还需要在研究

import matplotlib.pyplot as plt
import numpy as np

x = [-20, 1, 2, 3, 4, 7, 8, 12, 14, 10]
y = [-20, 1, 2, 3, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 8, 9, 10]
c = ['AAAA']
plt.boxplot(x, sym='*', notch=False, whis=1.5, positions=(2,),
            patch_artist=True, labels=c, manage_ticks=True,
            autorange=False, showmeans=True, showfliers=True,
            boxprops={'edgecolor':'black', 'facecolor':'cyan','linestyle':':','linewidth':'4'},
            capprops={'linestyle':':','linewidth':'4','color':'red'},
            whiskerprops={'linestyle':'--','linewidth':'3','color':'green'},
            flierprops={'markersize':10,'mfc':'r'},
            medianprops={'linestyle':'--','linewidth':'2','color':'blue'},
            meanprops={'markerfacecolor':'gold','markersize':'20'})
plt.show()

在这里插入图片描述

官方文档:boxplot函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值