Pandas 一个数据点上的箱线图错误
Pandas box plot error on one datapoint
我正在使用 pandas 绘制箱线图。
我的 DataFrame 看起来像这样
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
我的图表代码如下所示
chartDF.boxplot(grid=False, figsize=(9,4))
导致情节看起来像这样
我对 2017 年较低的数值感到困惑。
有谁知道如何解决这个问题?
以下情况会怎样?
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 2.1, 2.3]})
df.boxplot()
它从这四个值中计算出分位数,值 1 被解释为异常值。因此:您使用的实际索引将被忽略,pandas 将这些值作为数据点。
因此,实际上,与其自己设置最小值、最大值和分位数,不如将完整数据从数据帧传递到箱线图。
这不是问题。箱线图基本上显示平均值周围的值。如果你有一个点超出矩形,则意味着具有该值的点是异常值。
有关离群值的更多信息:Outlier
这是 2017 年的异常值。如果您对该观察结果不感兴趣,只需在数据集中删除它即可,因为它会改变相应统计指标的值。
这是预期的行为。 2017 年的最小值比提供的四个数据点的第一个四分位数低 1.5 IQR 以上,在这种情况下,最小值显示为异常值(一个点)。
来自 the docs for whis
in boxplot
(强调我的):
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')
我正在使用 pandas 绘制箱线图。
我的 DataFrame 看起来像这样
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
我的图表代码如下所示
chartDF.boxplot(grid=False, figsize=(9,4))
导致情节看起来像这样
我对 2017 年较低的数值感到困惑。
有谁知道如何解决这个问题?
以下情况会怎样?
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 2.1, 2.3]})
df.boxplot()
它从这四个值中计算出分位数,值 1 被解释为异常值。因此:您使用的实际索引将被忽略,pandas 将这些值作为数据点。
因此,实际上,与其自己设置最小值、最大值和分位数,不如将完整数据从数据帧传递到箱线图。
这不是问题。箱线图基本上显示平均值周围的值。如果你有一个点超出矩形,则意味着具有该值的点是异常值。
有关离群值的更多信息:Outlier
这是 2017 年的异常值。如果您对该观察结果不感兴趣,只需在数据集中删除它即可,因为它会改变相应统计指标的值。
这是预期的行为。 2017 年的最小值比提供的四个数据点的第一个四分位数低 1.5 IQR 以上,在这种情况下,最小值显示为异常值(一个点)。
来自 the docs for whis
in boxplot
(强调我的):
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 thanQ3 + whis*IQR)
. Similarly, the lower whisker will extend to the first datum greater thanQ1 - 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')