Skip to content

Instantly share code, notes, and snippets.

@avapolzin
Last active August 16, 2026 02:08
Show Gist options
  • Select an option

  • Save avapolzin/f4824060d4c579f37619cfd4d3f98229 to your computer and use it in GitHub Desktop.

Select an option

Save avapolzin/f4824060d4c579f37619cfd4d3f98229 to your computer and use it in GitHub Desktop.
Using dropdowns to toggle which traces are shown with Plotly
"""
I'm sure others have figured this out, too, but on a search I found no answers on any fora that actually addressed how to use
Plotly's built-in features to show individual traces without impacting the visibility of the others. The example is taken directly
from an interactive I made (you can see the example here: https://questdwarfs.github.io/assets/html/quest_compare_spectra.html), so
some of what is here will be extraneous for the one point (and I'm sure there are inefficiencies here as I only ever use Plotly
occasionally). The steps are roughly:
1) generate your traces with visibility toggled off (this is also part of the Plotly tutorial)
2) instead of using *update* in your dropdown, you'll use *relayout*, which will let you specify which tracers are impacted
when you change visible to True for the trace of interest
That's really it. It's actually super simple, so it's disappointing that this question is so common but the answer is so hard to find.
Hope this code snippet helps a little bit :)
"""
import plotly.graph_objects as go
fig = go.Figure()
fig.update_layout(yaxis_title = 'normalized flux',
xaxis_title = 'rest-frame wavelength (Å)',
paper_bgcolor="white", plot_bgcolor="white",
xaxis_linecolor = 'black', yaxis_linecolor = 'black',
xaxis_gridcolor = 'lightgray', yaxis_gridcolor = 'lightgray',
xaxis_mirror = True, yaxis_mirror = True,
width = 790, height = 300, margin=dict(l=0,r=0,b=0,t=30), font_family='Open-Sherif')
for sk in list(compspec.keys()):
fig.add_trace(go.Scatter(x = compspec[sk]['wave'], y = compspec[sk]['spec'], line = {'shape':'hvh', 'color':'midnightblue'},
visible = False, name = sk, showlegend = False))
for sk in list(compspec.keys()):
fig.add_trace(go.Scatter(x = compspec[sk]['wave'], y = compspec[sk]['spec'], line = {'shape':'hvh', 'color':'mediumvioletred'},
visible = False, name = sk, showlegend = False))
for sk in list(compspec.keys()):
fig.add_trace(go.Scatter(x = compspec[sk]['wave'], y = compspec[sk]['spec'], line = {'shape':'hvh', 'color':'deepskyblue'},
visible = False, name = sk, showlegend = False))
for sk in list(compspec.keys()):
fig.add_trace(go.Scatter(x = compspec[sk]['wave'], y = compspec[sk]['spec'], line = {'shape':'hvh', 'color':'#EE7F2D'},
visible = False, name = sk, showlegend = False))
fig.update_xaxes(range = [3600, 9000])
fig.update_layout(
updatemenus=[
{ "active": 0,
"pad":{'r':3},
"buttons": [
{
"label": f"{gal}",
"method": "restyle",
"args": [
{"visible": np.where(np.array(list(compspec.keys())) == gal, True, False),
'width':0, 'thickness':0.75},
[i for i in range(len(compspec.keys()))],
],
}
for gal in list(compspec.keys())
],
"x": 1.4,
"y": 0.9,
"font":{'color':'midnightblue'}
},
{ "active": 0,
"pad":{'r':3},
"buttons": [
{
"label": f"{gal}",
"method": "restyle",
"args": [
{"visible": np.where(np.array(list(compspec.keys())) == gal, True, False),
'width':0, 'thickness':0.75},
[i + len(compspec.keys()) for i in range(len(compspec.keys()))],
],
}
for gal in list(compspec.keys())
],
"x": 1.4,
"y": 0.7,
"font":{'color':'mediumvioletred'}
},
{ "active": 0,
"pad":{'r':3},
"buttons": [
{
"label": f"{gal}",
"method": "restyle",
"args": [
{"visible": np.where(np.array(list(compspec.keys())) == gal, True, False),
'width':0, 'thickness':0.75},
[i + 2*len(compspec.keys()) for i in range(len(compspec.keys()))],
],
}
for gal in compspec.keys()
],
"x":1.4,
"y": 0.5,
"font":{'color':'deepskyblue'}
},
{ "active": 0,
"pad":{'r':3},
"buttons": [
{
"label": f"{gal}",
"method": "restyle",
"args": [
{"visible": np.where(np.array(list(compspec.keys())) == gal, True, False),
'width':0, 'thickness':0.75},
[i + 3*len(compspec.keys()) for i in range(len(compspec.keys()))],
],
}
for gal in compspec.keys()
],
"x":1.4,
"y": 0.3,
"font":{'color':'#EE7F2D'}
},
],
paper_bgcolor="white", plot_bgcolor="white",
width = 790, height = 300, margin=dict(l=0,r=0,b=0,t=30),
font_family='Open-Sherif'
)
fig.add_annotation(x = 1.22, y = 1.01, text = r'Select spectra to show:', xref = "paper", yref = "paper", showarrow = False)
fig.show()
fig.write_html("quest_compare_spectra.html", include_mathjax = 'cdn')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment