Help in plotting with plotly.graph_objects

for (v,p) in active_x1:
fig.add_trace( go.Scattermapbox(
name=‘Arc_x1’,
mode=“lines”,
lat=[G.nodes[v][‘lat’] , G.nodes[p][‘lat’]],
lon=[G.nodes[v][‘lon’] , G.nodes[p][‘lon’]],
marker=dict(size=10, color=‘black’),
text= ‘[’+ str(v)+ ‘][’+str(p)+‘]’,
legendgroup=‘Arcs’,
))

what should I do if I want to plot just 1 legend for all these traces?

Hi @buzzo123 welcome to the forums.

What exactly are you referring to? You want to have only one Arc_x1 entry?

Maybe showlegend=False is what you are searching for.

yes, I want to plot only Arc_x1 exactly once

You have two options. You could use thelegendgrouptitle and set an empty string to the name or set the name and don’t use the legendgrouptitle

In either solution you have to set the showlegend=False initially and change that in one of the traces to showlegend=True afterwards.

import plotly.graph_objects as go
import numpy as np

cols = 3
rows = 9

# create data
a = np.repeat(np.arange(1, rows+1), cols, axis=-1)
a = a.reshape((rows, cols))

# create figure
fig = go.Figure()
for idx in range(rows):
    fig.add_trace(
            go.Scatter3d(
                x=[a[idx, 0]], 
                y=[a[idx, 1]],
                z=[a[idx, 2]],
                name='',
                mode='markers',
                marker_color='red',
                showlegend=False,
                legendgroup='Arc_x1',
                legendgrouptitle={'text':'Arc_x1'}
            ),
    )
#
fig.update_traces({'showlegend':True}, selector=0)
fig.show()
import plotly.graph_objects as go
import numpy as np

cols = 3
rows = 9

# create data
a = np.repeat(np.arange(1, rows+1), cols, axis=-1)
a = a.reshape((rows, cols))

# create figure
fig = go.Figure()
for idx in range(rows):
    fig.add_trace(
            go.Scatter3d(
                x=[a[idx, 0]], 
                y=[a[idx, 1]],
                z=[a[idx, 2]],
                name='Arc_x1',
                mode='markers',
                marker_color='red',
                showlegend=False,
                legendgroup='Arc_x1',
                #legendgrouptitle={'text':'Arc_x1'}
            ),
    )
#
fig.update_traces({'showlegend':True}, selector=0)
fig.show()
1 Like