Search code examples
pythonpython-3.xplotlyplotly-python

Numbers are not visible in Python Plotly Funnel


I am plotting a funnel in plotly using python, and even though I have adjusted the font size to 18, the numbers at the really bottom for the red part are very small and arent visible. I am wondering how I can adjust it so that the numbers are on the outside like they are for the green part of the funnel

enter image description here


Solution

  • Unfortunately I don't think there is an easy way to achieve what you want. You could try to hide the text and use annotations, but this isn't easily generalizable as you would need to know ahead of time which values are too small for the text to fit legibly inside of the chart.

    The best solution I can think of is to use the constraintext = "outside" parameter to force the text to stay the same size regardless of the width for each stage of the funnel, although the text on the left will still be inside the chart.

    As you pointed out, my original suggestion of using textposition = "outside" doesn't work properly (as it places the left text on the right side of the chart), and I believe that this is a bug in the Plotly library.

    from plotly import graph_objects as go
    
    fig = go.Figure()
    
    fig.add_trace(go.Funnel(
        y = ['a','b','c','d','e','f'],
        x = [135, 62, 47, 20, 2, 1],
        constraintext='outside',
        textfont=dict(
            family="sans serif",
            size=18,
            color="white"
            )
        )
    )
    
    fig.add_trace(go.Funnel(
        orientation = "h",
        y = ['a','b','c','d','e','f'],
        x = [298, 145, 120, 87, 11, 2], 
        constraintext='outside',
        textfont=dict(
            family="sans serif",
            size=18,
            color="black"
            )
        )
    )
    
    fig.show()
    

    enter image description here