如何指定PYPLATE框中的百分位数?

问题描述

假设我有最简单的脚本

import plotly.offline as pyo
import plotly.graph_objs as go

# set up an array of 20 data points, with 20 as the median value
y = [1,14,14,15,16,18,18,19,19,20,20,23,24,26,27,27,28,29,33,54]

data = [
    go.Box(
        y=y,
        boxpoints='outliers' # display only outlying data points
    )
]
pyo.plot(data, filename='box2.html')

在此我有以下内容

我的问题是,我知道这是针对25%和75%的人的? 是否有方法可以更改显示的百分位数的值?


解决方案

  • 根据其他评论和答案。四分位数是四分位数,可以用其他方法计算
  • 如果除四分位数外不需要百分位数,则可以在图中添加额外的线条

下面演示了如何在该图中添加9条百分位线

import plotly.graph_objs as go
import numpy as np
import pandas as pd
import plotly.express as px

# set up an array of 20 data points, with 20 as the median value
y = [1,14,14,15,16,18,18,19,19,20,20,23,24,26,27,27,28,29,33,54]

data = [
    go.Box(
        y=y,
        boxpoints='outliers' # display only outlying data points
    )
]

fig = go.Figure(data) #.add_traces(go.Bar(x=np.repeat(["trace 0"],9), y=[np.percentile(y, q) for q in np.linspace(10,90,9)]))

fig.add_traces(
    px.line(
        pd.DataFrame(
            {
                "y": np.repeat(np.percentile(y, np.linspace(10, 90, 9)), 2),
                "x": np.tile([0.4, 0.6], 9),
                "p": np.repeat(np.linspace(10, 90, 9), 2),
            }
        ),
        x="x",
        y="y",
        color="p",
    )
    .update_traces(showlegend=False, xaxis="x2")
    .data
).update_layout(xaxis2={"overlaying": "x", "visible": False, "range": [0, 1]})

相关文章