Double-click legend to isolate traces and then download selected data in a csv

HI @dash_newb welcome to the forums.

I think by double clicking a trace in the legend you are actually not selecting anything, you are just changing the visibility of the corresponding trace in the figure.

You could check for the visible state of the trace and export only the visible traces. Here is something related to that:

An example:

import plotly.graph_objects as go

# create figure
fig = go.Figure()

# add traces
fig.add_trace(go.Scatter(x=[1,2,3], y=[1,2,3], visible='legendonly'))
fig.add_trace(go.Scatter(x=[1,2,3], y=[4,5,6], visible=True))

# extract only visible traces
visible_trace = [trace for trace in fig.select_traces(selector={'visible':True})]

fig.show()

newplot (11)

print(visible_trace)

[Scatter({
    'visible': True, 'x': [1, 2, 3], 'y': [4, 5, 6]
})]
2 Likes