# Plotly Dropdown Menus and Buttons with updatemenus

> How to add a Plotly dropdown menu or buttons with updatemenus, using the restyle, relayout and update methods to switch traces, chart types and axis settings.

Source: https://www.pythoncharts.com/plotly/plotly-dropdown-menus-buttons/ | Published: 2026-11-27 | Author: Alex Scott | Category: Plotly | Tags: plotly, updatemenus, dropdown, buttons, interactive, layout

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:

```python
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()
```

![Bar chart of monthly rainfall in Port Avery, highest in winter, with a dropdown menu to switch between three cities](https://www.pythoncharts.com/plotly/plotly-dropdown-menus-buttons/images/plotly-dropdown-quick-example.png)

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.

```python
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()
```

![Line chart of monthly rainfall with a dropdown that swaps the city's values and line color](https://www.pythoncharts.com/plotly/plotly-dropdown-menus-buttons/images/plotly-dropdown-restyle.png)

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.

```python
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()
```

![Scatter chart of Lakewood rainfall with Markers, Line and Bars buttons above it](https://www.pythoncharts.com/plotly/plotly-dropdown-menus-buttons/images/plotly-dropdown-chart-type.png)

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.

```python
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()
```

![Bar chart of accounts by plan, from 12,000 Starter to 35 Enterprise, with Linear and Log axis buttons](https://www.pythoncharts.com/plotly/plotly-dropdown-menus-buttons/images/plotly-dropdown-log.png)

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`.

```python
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()
```

![Plotly Express line chart of rainfall in three cities with a dropdown to show all cities or one](https://www.pythoncharts.com/plotly/plotly-dropdown-menus-buttons/images/plotly-dropdown-express.png)

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.

```python
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()
```

![Bar chart of monthly rainfall with one dropdown for the city and another for the bar color](https://www.pythoncharts.com/plotly/plotly-dropdown-menus-buttons/images/plotly-dropdown-two-menus.png)

## Position and Style the Menu

- `x` and `y` are fractions of the plot area, so `y` above 1 is in the top margin. Leave room with `margin_t`.
- `active=1` preselects the second button. It only highlights the button, so set the traces to match.
- `showactive=False` removes the highlight, which suits one-off actions such as "Reset".
- `bgcolor`, `bordercolor` and `font` style the menu, and `pad=dict(r=10, t=5)` adds space around it.

Static exports from [Kaleido](https://www.pythoncharts.com/plotly/plotly-export-static-images-with-kaleido/) draw the menu but only the initial state. To share the interactive version, [save the figure as HTML](https://www.pythoncharts.com/plotly/plotly-saving-standalone-html-files/).

## 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](https://www.pythoncharts.com/plotly/range-slider-selector-time-series/) | Stepping through time | Same as above |
| Dash or [Streamlit](https://www.pythoncharts.com/streamlit/streamlit-charts-compared/) | Filtering large data, queries, several linked charts | Needs a running server |

## Similar Topics

- [Adding Range Sliders and Selectors to a Time Series Plot in Plotly](https://www.pythoncharts.com/plotly/range-slider-selector-time-series/)
- [Animated Bubble Charts over Time in Plotly](https://www.pythoncharts.com/plotly/animated-bubble-charts/)
- [Saving Interactive Plotly Figures as Standalone HTML Files](https://www.pythoncharts.com/plotly/plotly-saving-standalone-html-files/)
