Plotly Funnel - 不想显示“总数的%”,但确实想要“如果初始则为%”和“先前的%”

问题描述 投票:0回答:1

在下图中,您会看到悬停并且它具有“占总数的%”。该数字是所有级别加起来的百分比,这与此漏斗无关。

这是生成悬停的代码:

def create_engagement_figure(funnel_data=[]):

    fig = go.Figure(
        go.Funnel(
            y=funnel_data["Title"],
            x=funnel_data["Count"],
            textposition="auto",
            textinfo="value+percent initial+percent previous",
            # opacity=0.65,
            marker={
                "color": [
                    "#4F420A",
                    "#73600F",
                    "#947C13",
                    "#E0BD1D",
                    "#B59818",
                    "#D9B61C",
                ],
                "line": {
                    "width": [4, 3, 2, 2, 2, 1],
                    "color": ["wheat", "wheat", "wheat", "wheat"],
                },
            },
            connector={"line": {"color": "#4F3809", "dash": "dot", "width": 3}},
        )
    )
    fig.update_traces(texttemplate="%{value:,d}")
    fig.update_layout(
        margin=dict(l=20, r=20, t=20, b=20),
    )

    return fig

特别是这一行:

 textinfo="value+percent initial+percent previous",

我已经尝试过了

 textinfo="value+percent",
,
 textinfo="value",

并且所有内容仍然显示总数的%。有没有办法通过绘图库删除它,这样我就不必构建自定义悬停?构建自定义悬停是一个问题,因为此代码将基于传入的 DataFrame 构建多个级别的漏斗。

不太相关,这里是其余的代码:

def engagement_funnel_chart(daterange, countries_list):

    LR = metrics.get_totals_by_metric(daterange, countries_list, stat="LR")
    PC = metrics.get_totals_by_metric(daterange, countries_list, "PC")
    LA = metrics.get_totals_by_metric(daterange, countries_list, stat="LA")
    GC = metrics.get_totals_by_metric(daterange, countries_list, "GC")

    funnel_data = {
        "Title": [
            "Learners Reached",
            "Puzzle Completed",
            "Learners Acquired",
            "Game Completed",
        ],
        "Count": [LR, PC, LA, GC],
    }
    fig = create_engagement_figure(daterange, countries_list, funnel_data)
    st.plotly_chart(fig, use_container_width=True)
python charts plotly plotly-python
1个回答
0
投票

您可以自定义

hoverinfo
属性(与
textinfo
一样,不必完全重写
hovertemplate
)。默认值为
'all'
,对应flaglist:

'x+y+text+percent initial+percent previous+percent total'

您想要删除

percent total
标志,例如。

fig = go.Figure(
    go.Funnel(
        y = ["Website visit", "Downloads", "Potential customers", "Requested price", "Finalized"],
        x = [39, 27.4, 20.6, 11, 2],
        textposition="auto",
        textinfo="value+percent initial+percent previous",

        hoverinfo='x+y+text+percent initial+percent previous',

        # opacity=0.65,
        marker={
            "color": [
                "#4F420A",
                "#73600F",
                "#947C13",
                "#E0BD1D",
                "#B59818",
                "#D9B61C",
            ],
            "line": {
                "width": [4, 3, 2, 2, 2, 1],
                "color": ["wheat", "wheat", "wheat", "wheat"],
            },
        },
        connector={"line": {"color": "#4F3809", "dash": "dot", "width": 3}},
    )
)
© www.soinside.com 2019 - 2024. All rights reserved.