Search code examples
pythonplotlyplotly-python

Joining traces in plotly


I am trying to make a shape conformed by a few traces, many of which are not continuous. By the moment, I've managed to create this shape with different traces.

I've tried calling them the same name, but this didn't work. For instance:

import plotly.graph_objects as go
fig = go.Figure(go.Scatter(x=[1,2,0], y=[3,1,1],name="Name"))
fig.add_trace(go.Scatter(x=[0,1,2], y=[0,2,0],name="Name"))
fig.show()

enter image description here

Is it possible to plot this shape into an unique trace?


Solution

  • In Plotly, if you have two independent segments belong to the same shape, and you want to plot each one without any connection, you must do that with 2 traces but you can color the segments of the same shape with the same color and use legendgroup to control all segment under the same shape:

    import plotly.graph_objects as go
    
    fig = go.Figure(go.Scatter(x=[1,2,0], y=[3,1,1],
                    marker_color="blue", legendgroup="1", name="Shape1"))
    fig.add_trace(go.Scatter(x=[0,1,2], y=[0,2,0],
                    marker_color="blue", legendgroup="1", showlegend=False))
    fig.show()
    

    enter image description here