【问题标题】:Plotly box plot turn off outlier detection绘制箱线图关闭异常值检测
【发布时间】:2019-12-13 21:34:13
【问题描述】:

在 Plotly (Python) 中,箱线图默认检测离群值,如果有它判定为离群值,则胡须不会扩展到离群值。但是,我知道我的任何数据点都不应该被视为异常值。是否可以关闭箱线图中的异常值检测,并将整个数据集视为异常值?

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

【问题讨论】:

    标签: python plotly boxplot outliers


    【解决方案1】:

    目前看来,这样做的唯一方法是使用多条轨迹并将它们调整到相同的位置,如下图和 sn-p 所示。如果您想了解一些细节,请查看最后的 sn-ps 和绘图。

    在下面的 sn-p 中,我将 go.Box(x=x0) 用于两条不同的迹线,它们具有相同的数据,但标记和线的设置不同,以实现此目的:

    剧情:

    代码:

    # 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,则行将不包含异常值:

    情节:默认

    代码:

    # 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 删除所有箱点

    剧情:


    代码:

    # 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()
    

    当然,这不是你的目标。

    我希望这会有所帮助。如果没有,请随时告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      • 2019-12-22
      • 2021-11-06
      • 1970-01-01
      相关资源
      最近更新 更多