Double-click legend to isolate traces when some traces have showlegend=False

Hi,

I’m plotting some traces with mode=‘lines’ and showlegend=True, but then also adding traces for the final data point of each line with mode=‘markers’ and showlegend=False. Here’s an example:

trace_data = pd.DataFrame({'base': [0, 1, 2, 3, 4],
                           'trace1': [1, 2, 3, 4, 5],
                           'trace2': [1.5, 1.9, 2, 3.9, 6],
                           'trace3': [2, 1, 3.3, 4.4, 4.5],
                           'trace4': [0, 3, 2, 1, 3]})

trace_colors = plotly.colors.qualitative.D3
color_idx = 0

fig = go.Figure()
for trace in ['trace1', 'trace2', 'trace3', 'trace4']:
    # Perform a lot of data wrangling in Pandas here...
    fig.add_trace(go.Scatter(x=trace_data['base'],
                             y=trace_data[trace],
                             mode='lines',
                             marker=dict(color=trace_colors[color_idx % len(trace_colors)]),
                             name=trace))
    fig.add_trace(go.Scatter(x=[trace_data['base'].iloc[-1]],
                             y=[trace_data[trace].iloc[-1]],
                             mode='markers',
                             marker=dict(color=trace_colors[color_idx % len(trace_colors)]),
                             showlegend=False))
    color_idx += 1
fig.show()

The trouble is, when I double-click on one of the legend entries in order to isolate that trace, the end-markers for all traces are still visible:


This is actually much, much more distracting with my actual dataset. I’m also plotting the traces in that loop because with my actual data there’s a lot of pre-processing I need to do within the loop before adding the trace.

Is there a better way to add these end-point markers? And if not, how can I isolate a trace by double-clicking the legend when I’ve got these hidden legend markers?

Thanks!!!

Hi @gregrafferty, welcome to the forum! You can do this by plotting lines + markers for all your traces, and impose that only the last marker has a non-zero size.

import pandas as pd
import plotly
import plotly.graph_objects as go
trace_data = pd.DataFrame({'base': [0, 1, 2, 3, 4],
                           'trace1': [1, 2, 3, 4, 5],
                           'trace2': [1.5, 1.9, 2, 3.9, 6],
                           'trace3': [2, 1, 3.3, 4.4, 4.5],
                           'trace4': [0, 3, 2, 1, 3]})

trace_colors = plotly.colors.qualitative.D3
color_idx = 0
size = [0., 0., 0., 0., 10]

fig = go.Figure()
for trace in ['trace1', 'trace2', 'trace3', 'trace4']:
    # Perform a lot of data wrangling in Pandas here...
    fig.add_trace(go.Scatter(x=trace_data['base'],
                             y=trace_data[trace],
                             mode='lines+markers',
                             marker=dict(color=trace_colors[color_idx % len(trace_colors)]),
                             marker_size=size,
                             name=trace))
    color_idx += 1
fig.show()
1 Like