Joining traces in plotly

Question:

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?

Asked By: user20167936

||

Answers:

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

Answered By: Hamzah
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.