Search code examples
pythonchartsplotlyplotly-python

Plotly Funnel - Don't want to display "% of total" but do want "% if initial" and "% of previous"


In the following image, you see the hover and it has "% of total". That number is a % of all of the levels added up, which is irrelevant in this funnel.

Plotly funnel with hover funnel percentages

Here is the code that generates the hover:

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

In particular the line: textinfo="value+percent initial+percent previous",

I've tried textinfo="value+percent",, textinfo="value",

and everything still shows % of total. Is there a way to remove it via the plotly library so I don't have to build a custom hover? Building a custom hover is a problem because this code will build funnels of many levels based on the DataFrame that is passed in.

Less relevant, here is the rest of the code:

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)

Solution

  • You can customize the hoverinfo property (like for textinfo, don't have to rewrite hovertemplate entirely). The default value is 'all', which corresponds to the flaglist :

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

    You want to remove the percent total flag, eg.

    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}},
        )
    )