绘图箱图关闭异常值检测

时间:2019-08-06 10:40:48

标签: python plotly boxplot outliers

在Plotly(Python)中,箱形图默认情况下会检测离群值,并且如果存在决定离群的值,则晶须不会扩展到离群值。但是,我知道我的所有数据点都不应该被视为离群值。是否可以关闭箱形图中的离群值检测,并将整个数据集视为离群值?

顺便说一句,我仍然想在箱形图旁边显示所有点,所以我不想使用选项boxpoints=False来强制箱形图包括所有点。

1 个答案:

答案 0 :(得分:1)

目前看来,唯一的方法就是使用多重轨迹并将其调整到相同的位置,如下图所示。如果您需要一些细节,请查看最后的代码片段和情节。

在以下代码段中,我将go.Box(x=x0)用于两条不同的迹线,这些迹线具有相同的数据,但标记和行的设置不同,以实现此目的:

情节:

enter image description here

代码:

# imports
import plotly
from plotly import tools
import pandas as pd
import numpy as np
import plotly.graph_objs as go

# setup
np.random.seed(123)

# data
y0 = np.random.randn(50)-1
x0 = y0
x0 = [0 for y in y0]

# include an outlier
y0[-1] = 4

# traces
trace0 = go.Box(x=x0,
                y=y0, boxpoints = False, pointpos = 0,
                marker = dict(color = 'rgb(66, 167, 244)'),
)

trace1 = go.Box(x=x0,
                y=y0, boxpoints = 'all', pointpos = 0,
                marker = dict(color = 'rgb(66, 66, 244)'),
                line = dict(color = 'rgba(0,0,0,0)'),
                fillcolor = 'rgba(0,0,0,0)'
)

data=[trace0, trace1]

# figure
fig = go.Figure(data)
fig.show()

有关默认行为的详细信息:

如果未指定Boxpoints,则这些行将不包含异常值:

图解:默认

enter image description here

代码:

# imports
import plotly
from plotly import tools
import pandas as pd
import numpy as np
import plotly.graph_objs as go

# setup
np.random.seed(123)

# data
y0 = np.random.randn(50)-1
y0[-1] = 4

# traces
trace0 = go.Box(y=y0, pointpos = 0,
                marker = dict(color = 'rgb(66, 167, 244)'),

)

# figure
fig = go.Figure(trace0)
fig.show()

使行包括异常值的唯一方法是通过设置boxpoints = False

删除所有箱形点

情节:

enter image description here


代码:

# imports
import plotly
from plotly import tools
import pandas as pd
import numpy as np
import plotly.graph_objs as go

# setup
np.random.seed(123)

# data
y0 = np.random.randn(50)-1
y0[-1] = 4

# traces
trace0 = go.Box(y=y0, pointpos = 0,
                marker = dict(color = 'rgb(66, 167, 244)'),
                boxpoints = False
)

# figure
fig = go.Figure(trace0)
fig.show()

当然,这不是您的目标。

我希望这会有所帮助。如果没有,那就不要犹豫,让我知道。