Search code examples
plotlyplotly.graph-objects

my go.Scatter text doesnt work but my figure does


I try to do some graphs with plotly.graph_object.

My figure works but not my text.

fig.add_trace(go.Scatter(x=[5,15,15,5],
    
                            y=[0,0,niveau_coupon[1], niveau_coupon[0]],
                            
                            fill='toself',
                            fillcolor='grey',
                            line=dict(width=0),
                            showlegend=False,
                            mode='lines',  
                            hoverinfo ='none',
                            text=["testttttttttttt"],
                  textposition='top right',
                  textfont=dict(color='white'),
))

Thanks for yours answers


Solution

  • The key change mode="lines+text" Also changed

    • position to make it visible
    • list length 4 so each point has some text
    • from comments you not you want text add middle of area. This therefore means it is an annotation. Provided example
    import plotly.graph_objects as go
    import math
    
    fig = go.Figure()
    niveau_coupon = [10, 20]
    
    fig.add_trace(
        go.Scatter(
            x=[5, 15, 15, 5],
            y=[0, 0, niveau_coupon[1], niveau_coupon[0]],
            fill="toself",
            fillcolor="grey",
            line=dict(width=0),
            showlegend=False,
            mode="lines+text",
            hoverinfo="none",
            text=["testttttttttttt"] * 4,  # one for each point..
            textposition="top center",  # changed to make it visible on right
            textfont=dict(color="white"),
        )
    )
    
    fig.add_annotation(
        x=sum(fig.data[0].x) / len(fig.data[0].x),
        y=sum(fig.data[0].y) / len(fig.data[0].y),
        text="test",
        showarrow=False,
        font=dict(color="white")
    )
    

    enter image description here