关于python:matplotlib中的Boxplots:标记和异常值

Boxplots in matplotlib: Markers and outliers

我对matplotlib中的箱线图有一些疑问:

问题A.我在下面用Q1,Q2和Q3突出显示的标记代表什么? 我相信Q1是最大值,而Q3是离群值,但是Q2是什么?

enter image description here

问题B matplotlib如何识别异常值? (即,如何知道它们不是真正的maxmin值?)


一张图片胜过千言万语。请注意,离群值(绘图中的+标记)只是位于下方宽[(Q1-1.5 IQR), (Q3+1.5 IQR)]边距之外的点。

enter image description here

但是,图片仅是正态分布数据集的示例。重要的是要了解,matplotlib不会首先估计正态分布,而是根据估计的分布参数计算四分位数,如上所示。

相反,中位数和四分位数直接从数据中计算得出。因此,根据数据的分布和样本的大小,您的箱线图可能看起来有所不同,例如,不对称且具有或多或少的异常值。


该框表示第一和第三四分位数,红线表示中位数(第二四分位数)。该文档给出了默认的晶须为1.5 IQR:

1
2
3
boxplot(x, notch=False, sym='+', vert=True, whis=1.5,
        positions=None, widths=None, patch_artist=False,
        bootstrap=None, usermedians=None, conf_intervals=None)

whis : [ default 1.5 ]

Defines the length of the whiskers as a function of the inner quartile range. They extend to the most extreme data point within ( whis*(75%-25%) ) data range.

如果您对不同的箱形图表示感到困惑,请尝试阅读Wikipedia中的描述。


除了seth答案(因为有关此文档不是很精确):
Q1(晶须)的最大值低于75%+ 1.5 IQR

(最小值为25%-1.5 IQR)

这是计算晶须位置的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        # get high extreme
        iq = q3 - q1
        hi_val = q3 + whis * iq
        wisk_hi = np.compress(d <= hi_val, d)
        if len(wisk_hi) == 0 or np.max(wisk_hi) < q3:
            wisk_hi = q3
        else:
            wisk_hi = max(wisk_hi)

        # get low extreme
        lo_val = q1 - whis * iq
        wisk_lo = np.compress(d >= lo_val, d)
        if len(wisk_lo) == 0 or np.min(wisk_lo) > q1:
            wisk_lo = q1
        else:
            wisk_lo = min(wisk_lo)


下图显示了箱线图的不同部分。

enter image description here

分位数1 / Q1:25%

四分位间距(IQR):第25个百分点至第75个百分点。

中位数(第2 / Q2位数):第50个百分位数。

分位数3 / Q3:第75个百分位。

我应该注意,蓝色部分是箱线图的晶须。

下图将正态分布的箱形图与概率密度函数进行了比较。它应该有助于解释"最小","最大"和离群值。

enter image description here

"最低":(Q1-1.5 IQR)

"最大值":(Q3 + 1.5 IQR)

正如zelusp所说,正态分布的数据中29.3698σ(标准偏差)内包含99.3%的数据。下图中的绿色圆圈(异常值)是数据的剩余0.7%。这是这些数字的来源。


这是一张图表,显示了stats.stackexchange答案中的框的组成部分。请注意,如果您未在Pandas中提供whis关键字,则k = 1.5。

enter image description here

Pandas中的boxplot函数是matplotlib.pyplot.boxplot的包装。 matplotlib文档详细解释了盒子的组成部分:

问题A:

The box extends from the lower to upper quartile values of the data, with a line at the median.

一世。 e。输入数据值的四分之一在该框下方,四分之一在该框上方。

问题B:

whis : float, sequence, or string (default = 1.5)

As a float, determines the reach of the whiskers to the beyond the
first and third quartiles. In other words, where IQR is the
interquartile range (Q3-Q1), the upper whisker will extend to last
datum less than Q3 + whis*IQR). Similarly, the lower whisker will
extend to the first datum greater than Q1 - whis*IQR. Beyond the
whiskers, data are considered outliers and are plotted as individual
points.

Matplotlib(和Pandas)还为您提供了许多选项来更改该晶须的默认定义:

Set this to an unreasonably high value to force the whiskers to show
the min and max values. Alternatively, set this to an ascending
sequence of percentile (e.g., [5, 95]) to set the whiskers at specific
percentiles of the data. Finally, whis can be the string 'range' to
force the whiskers to the min and max of the data.


万一这可以使其他人受益,我需要在我的一个箱形图上放一个图例,以便在Inkscape中制作这个小.png并认为我会分享。

enter image description here