matplotlib中的box图:标记和异常值

2024-09-28 20:56:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我对matplotlib中的boxplots有一些问题:

问题A。下面我用Q1Q2Q3突出显示的标记代表什么?我认为Q1是最大值,Q3是异常值,但什么是Q2

enter image description here

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


Tags: 标记matplotlib代表minmaxq3q2q1
3条回答

一幅画胜过千言万语。请注意,离群值(图中的+标记)只是指下方宽[(Q1-1.5 IQR), (Q3+1.5 IQR)]边距的外部点。

enter image description here

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

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

方框表示第一和第三个四分位数,红线表示中间值(第二个四分位数)。documentation给出了1.5 IQR下的默认晶须:

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.

如果您对不同的方框图表示法感到困惑,请尝试读取the description in wikipedia

这是一个图解,说明了来自stats.stackexchange answer的盒子的组件。注意,如果不在Pandas中提供whis关键字,k=1.5。

enter image description here

Pandas中的boxplot函数是matplotlib.pyplot.boxplot的包装器。matplotlib docs详细解释了盒子的组件:

问题A:

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

也就是说,四分之一的输入数据值在方框下方,四分之一在方框上方。

问题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.

相关问题 更多 >