Add a dropdown to a Plotly figure with fig.update_layout(updatemenus=[dict(buttons=[...])]). Each button has a label, a method ("restyle" for traces, "relayout" for the layout, "update" for both) and args describing the change. It all runs in the browser, so it still works in a saved HTML file.
Quick Example
One trace per city, with a dropdown that shows one at a time:
import numpy as np
import pandas as pd
import plotly.graph_objects as go
rng = np.random.default_rng(72)
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
wave = np.cos((np.arange(12) - 0.5) * np.pi / 6) # 1 in winter, -1 in summer
rain = pd.DataFrame({
city: (mean + swing * wave + rng.normal(0, 6, 12)).clip(3).round()
for city, mean, swing in [("Port Avery", 95, 45), ("Redmesa", 30, -18), ("Lakewood", 70, 5)]
}, index=months)
colors = {"Port Avery": "#0369a1", "Redmesa": "#c2410c", "Lakewood": "#15803d"}
fig = go.Figure()
for i, city in enumerate(rain.columns):
fig.add_bar(x=months, y=rain[city].tolist(), name=city,
marker_color=colors[city], visible=(i == 0))
buttons = [
dict(label=city, method="update",
args=[{"visible": [other == city for other in rain.columns]},
{"title.text": f"Monthly rainfall in {city} (mm)"}])
for city in rain.columns
]
fig.update_layout(
template="plotly_white",
title_text="Monthly rainfall in Port Avery (mm)",
yaxis_range=[0, 160],
showlegend=False,
updatemenus=[dict(buttons=buttons, x=1, xanchor="right", y=1.2, yanchor="top")],
margin_t=90,
)
fig.show()
Pick a city from the menu to swap the bars and the title. Port Avery is wettest in winter and Redmesa in summer. The fixed yaxis_range keeps the three comparable. The data is made up, and the code targets Plotly 6.x.
How updatemenus Buttons Work
method |
args |
Use it to change |
|---|---|---|
"restyle" |
[trace_changes, trace_indices] |
Data or style of traces |
"relayout" |
[layout_changes] |
Titles, axes, shapes, annotations |
"update" |
[trace_changes, layout_changes, trace_indices] |
Both in one click |
"animate" |
[frame_names, options] |
Play animation frames |
"skip" |
none | Nothing (for custom JavaScript) |
The trace indices are optional. Without them, the change applies to every trace.
In trace changes, a list is read one item per trace. {"visible": [True, False, False]} sets three traces. A data array therefore needs an extra pair of brackets: {"y": [new_values]} gives the whole list to one trace. Forgetting them is the most common reason a button does something strange.
Swap the Data with restyle
When the traces share a shape, keep one trace and replace its values. This avoids stacking hidden traces.
fig = go.Figure(go.Scatter(x=months, y=rain["Port Avery"].tolist(),
mode="lines+markers", line=dict(width=3, color="#0369a1")))
fig.update_layout(
template="plotly_white", title_text="restyle swaps y and color",
yaxis_range=[0, 160],
updatemenus=[dict(
x=1, xanchor="right", y=1.2, yanchor="top",
buttons=[dict(label=city, method="restyle",
args=[{"y": [rain[city].tolist()], "line.color": colors[city]}])
for city in rain.columns],
)],
margin_t=90,
)
fig.show()
The y list is wrapped in brackets because there's one trace, and "line.color" is a dotted path to a nested property.
Buttons Instead of a Dropdown
Set type="buttons" and direction="right" for a row of buttons. This one changes the chart type.
fig = go.Figure(go.Scatter(x=months, y=rain["Lakewood"].tolist(), mode="markers",
marker=dict(size=12, color="#15803d")))
fig.update_layout(
template="plotly_white", title_text="Rainfall in Lakewood (mm)",
yaxis_rangemode="tozero",
updatemenus=[dict(
type="buttons", direction="right", showactive=True,
x=0, xanchor="left", y=1.02, yanchor="bottom",
buttons=[
dict(label="Markers", method="restyle", args=[{"type": "scatter", "mode": "markers"}]),
dict(label="Line", method="restyle", args=[{"type": "scatter", "mode": "lines"}]),
dict(label="Bars", method="restyle", args=[{"type": "bar"}]),
],
)],
margin_t=110,
)
fig.show()
Switching type works between traces that use the same attributes, such as x, y and marker.color.
Toggle a Log Scale with relayout
relayout takes dotted layout keys such as "yaxis.type". An autorange request makes sure the axis rescales.
sales = {"Starter": 12000, "Team": 2600, "Business": 480, "Enterprise": 35}
fig = go.Figure(go.Bar(x=list(sales), y=list(sales.values()), marker_color="#6d28d9"))
fig.update_layout(
template="plotly_white", title_text="Accounts by plan",
updatemenus=[dict(
type="buttons", direction="right", x=1, xanchor="right", y=1.02, yanchor="bottom",
buttons=[
dict(label="Linear", method="relayout",
args=[{"yaxis.type": "linear", "yaxis.autorange": True}]),
dict(label="Log", method="relayout",
args=[{"yaxis.type": "log", "yaxis.autorange": True}]),
],
)],
margin_t=100,
)
fig.show()
On the linear scale the 35 Enterprise accounts are invisible next to 12,000 Starter accounts. The log button makes all four readable.
Filter Plotly Express Charts with a Dropdown
px functions create one trace per color group, so build the visible list from the trace names. An "All" button is a list of True.
import plotly.express as px
long = rain.rename_axis("month").reset_index().melt("month", var_name="city", value_name="mm")
fig = px.line(long, x="month", y="mm", color="city", color_discrete_map=colors,
template="plotly_white", title="Rainfall by city (mm)")
names = [trace.name for trace in fig.data]
buttons = [dict(label="All cities", method="restyle", args=[{"visible": [True] * len(names)}])]
buttons += [dict(label=city, method="restyle", args=[{"visible": [n == city for n in names]}])
for city in names]
fig.update_layout(updatemenus=[dict(buttons=buttons, x=1, xanchor="right",
y=1.2, yanchor="top")], margin_t=90)
fig.show()
With facet_col or a second grouping such as line_dash, each selection covers several traces. Match on trace.legendgroup in that case.
Use Two Menus at Once
updatemenus is a list. Give each menu its own position, and have each button change only its own property so the choices don't overwrite each other.
fig = go.Figure(go.Bar(x=months, y=rain["Port Avery"].tolist(), marker_color="#0369a1"))
city_menu = dict(
x=0, xanchor="left", y=1.2, yanchor="top",
buttons=[dict(label=c, method="restyle", args=[{"y": [rain[c].tolist()]}])
for c in rain.columns],
)
color_menu = dict(
x=0.25, xanchor="left", y=1.2, yanchor="top",
buttons=[dict(label=name, method="restyle", args=[{"marker.color": value}])
for name, value in [("Blue", "#0369a1"), ("Slate", "#475569"), ("Plum", "#86198f")]],
)
fig.update_layout(template="plotly_white", updatemenus=[city_menu, color_menu],
yaxis_range=[0, 160], margin_t=90)
fig.show()
Position and Style the Menu
xandyare fractions of the plot area, soyabove 1 is in the top margin. Leave room withmargin_t.active=1preselects the second button. It only highlights the button, so set the traces to match.showactive=Falseremoves the highlight, which suits one-off actions such as "Reset".bgcolor,bordercolorandfontstyle the menu, andpad=dict(r=10, t=5)adds space around it.
Static exports from Kaleido draw the menu but only the initial state. To share the interactive version, save the figure as HTML.
updatemenus vs Dash vs Streamlit
| Tool | Good for | Limit |
|---|---|---|
updatemenus |
A few fixed choices in a shareable HTML file | All data ships with the chart; no Python on click |
| Sliders and range selectors | Stepping through time | Same as above |
| Dash or Streamlit | Filtering large data, queries, several linked charts | Needs a running server |





