Search code examples
pythonplotly

Plotly two overlapping funnels: the code isn't working


I want to visualise two overlapping funnels: blue (wider one) and red. I want the blue one to have legends, numbers, axis invisible. So i tried this code (which is not working):

from plotly import graph_objects as go
from plotly.subplots import make_subplots
   

trace=go.Funnel(
    name = 'all',
    y = ["all leads", "not junks", "warms", "hots","deals"],
    x = [317379,304725,11476,3194,2163],
    textposition=None,
    marker={'xaxis': {'range': [0.2, 1],
                      'showgrid': False,
                      'zeroline': False, 
                      'visible': False}})


trace1=go.Funnel(
    name = 'web',
    y = ["all leads", "not junks", "warms", "hots","deals"],
    x = [281316,269490,10252,2508,1602],
    textposition='outside',
    marker={"color": '#dc143c',"colorscale": 'Hot',"colorbar": {"bgcolor": None}})

fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(trace)
fig.add_trace(trace1,secondary_y=True)

fig.show()

getting an error: Bad property path

The problem is with:

marker={'xaxis': {'range': [0.2, 1],
                      'showgrid': False,
                      'zeroline': False, 
                      'visible': False}})

I don't know how to fix, but i need it to make the blue one's legends, numbers and axes invisible. Without this the code is working, but returning this result: enter image description here Which is not smth i expect as it is not clear


Solution

  • If I understand correctly what you are trying to achieve, there is just a few edits needed to your code:

    from plotly import graph_objects as go
    from plotly.subplots import make_subplots
       
    trace=go.Funnel(
        name = 'all',
        y = ["all leads", "not junks", "warms", "hots","deals"],
        x = [317379,304725,11476,3194,2163],
        textposition=None,
        showlegend=False,
        marker={"color": 'blue'}
    )
    
    trace1=go.Funnel(
        name = 'web',
        y = ["all leads", "not junks", "warms", "hots","deals"],
        x = [281316,269490,10252,2508,1602],
        textposition='outside',
        marker={"color": '#dc143c',"colorscale": 'Hot',"colorbar": {"bgcolor": None}})
    
    fig = make_subplots(specs=[[{"secondary_y": True}]])
    fig.add_trace(trace)
    fig.add_trace(trace1, secondary_y=True)
    
    fig.layout.yaxis.update(showticklabels=False)
    fig.layout.xaxis.update({'visible': True})
    
    fig.for_each_trace(
        lambda trace: trace.update(ids=[]) if trace.name == "all" else (),
    )
    
    fig.show()
    

    Legend is removed for the blue/wider funnel chart with showlegend=False. Blue color is set for that chart with the marker= argument. y-axis is removed for that plot with fig.layout.yaxis.update(showticklabels=False). (You could remove the labels for the other y-axis with fig.layout.yaxis2.update(...).) And numbers are turned off for that plot with fig.for_each_trace(...).

    X-axis is also turned to visible with fig.layout.xaxis.update({'visible': True}).

    enter image description here