向 Plotly 箱线图添加计数

Adding counts to Plotly boxplots

我有一个相对简单的问题,但无法在网上找到任何解决该问题的答案。从一个简单的箱线图开始:

import plotly.express as px
 
df = px.data.iris()

fig = px.box(
    df, x='species', y='sepal_length'
)

val_counts = df['species'].value_counts()

我现在想将 val_counts(在此数据集中,每个物种 50 个)添加到图中,最好是在以下任一位置:

我怎样才能做到这一点?

使用我在这个答案中提出的相同方法:Change Plotly Boxplot Hover Data

  • 计算箱形图计算的所有度量加上您想要的附加度量count
  • 在箱形图轨迹上叠加条形轨迹,因此悬停具有所需的所有措施
import plotly.express as px

df = px.data.iris()

# summarize data as per same dimensions as boxplot
df2 = df.groupby("species").agg(
    **{
        m
        if isinstance(m, str)
        else m[0]: ("sepal_length", m if isinstance(m, str) else m[1])
        for m in [
            "max",
            ("q75", lambda s: s.quantile(0.75)),
            "median",
            ("q25", lambda s: s.quantile(0.25)),
            "min",
            "count",
        ]
    }
).reset_index().assign(y=lambda d: d["max"] - d["min"])

# overlay bar over boxplot
px.bar(
    df2,
    x="species",
    y="y",
    base="min",
    hover_data={c:not c in ["y","species"] for c in df2.columns},
    hover_name="species",
).update_traces(opacity=0.1).add_traces(px.box(df, x="species", y="sepal_length").data)

下面的代码片段将使用 fig.add_annotation 为最大行顶部 df['species'] 的所有唯一值设置 count = 50,如下所示:

for s in df.species.unique():
    fig.add_annotation(x=s,
                       y = df[df['species']==s]['sepal_length'].max(),
                       text = str(len(df[df['species']==s]['species'])),
                       yshift = 10,
                       showarrow = False
                      )

剧情:

完整代码:

import plotly.express as px
 
df = px.data.iris()

fig = px.box(
    df, x='species', y='sepal_length'
)

for s in df.species.unique():
    fig.add_annotation(x=s,
                       y = df[df['species']==s]['sepal_length'].max(),
                       text = str(len(df[df['species']==s]['species'])),
                       yshift = 10,
                       showarrow = False
                      )
f = fig.full_figure_for_development(warn=False)
fig.show()