一个数据点上的Pandas盒绘图错误

2024-09-19 23:46:02 发布

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

我正在用熊猫做一个盒子。在

我的数据框看起来像这样

Year                 2013      2014      2015      2016      2017
dfMin            1.091603  0.973346  1.040000  0.855209  1.079500
dfLowerQuartile  1.727191  1.684009  1.275601  1.136703  2.262654
dfUpperQuartile  2.225000  2.000000  1.857570  2.120644  2.435724
dfMax            2.687323  2.350000  2.105000  2.250000  2.566467

我的图表代码是这样的

^{pr2}$

导致这样的阴谋 enter image description here

我对2017年的较低价值表示困惑。在

有人知道如何解决这个问题吗?在


Tags: 数据代码图表year盒子价值阴谋pr2
3条回答

这是2017年的异常值。如果您对该观察结果不感兴趣,您只需在数据集中删除它,知道它会更改相应统计度量值的值。在

接下来会发生什么?在

import pandas as pd
df = pd.DataFrame({"a": [1, 2, 2.1, 2.3]})
df.boxplot()

它计算出这四个值中的分位数,值1被解释为离群值。所以:你使用的实际指数被忽略了,pandas将这些值作为数据点。在

因此,实际上,您不应该自己设置最小值、最大值和分位数,而是应该将数据帧中的完整数据传递到盒形图。在

这是预期的行为。您2017年的最小值比提供的四个数据点的第一个四分位数低1.5 IQR以上,在这种情况下,最小值显示为异常值(a点)。在

the docsboxplotemphasis mine):

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

所以如果你想让胡须一直延伸

df.boxplot(grid=False, figsize=(9, 4), whis='range')

enter image description here

相关问题 更多 >