Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 42 ยท Interactivity and Animation

Dropdown Menus and Buttons

Add dropdown menus and custom buttons to Plotly charts with updatemenus: restyle, relayout, update and animate methods, toggling traces, switching data and chart types.

A chart often has more than one useful view: revenue or profit, linear or log scale, bar or line, this year or last. Instead of making a separate figure for each, you can add controls inside the figure. Plotly calls them updatemenus: dropdown menus and button rows that change the chart when clicked. They work without any server: the browser applies the change itself, so they also work in an exported HTML file.

Everything an updatemenu does goes through one of four methods, and once you know them the rest is only layout.

The four methods

Method Changes args is
"restyle" Trace properties (data, visibility, color) [{"y": [[...]], "visible": [True, False]}, [trace indexes]]
"relayout" Layout properties (title, axis type, ranges) [{"yaxis.type": "log"}]
"update" Both at once [{trace changes}, {layout changes}]
"animate" Plays frames [frame names, {options}] (see animations)

Each button is a dictionary with a label, a method and its args. A menu has type="dropdown" (a collapsed list, the default) or type="buttons" (a row or column of always-visible buttons), plus placement: x, y, xanchor, yanchor, direction and pad.

Toggle traces with a dropdown

The most common recipe is "one chart, several series, show one at a time". Add all traces, make only the first visible, and give each button a visible list with exactly one True.

Python A dropdown that switches the visible series

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The "update" method changes the trace visibility and the title in one click. Notice that all three series are in the figure (so it is a little larger), only one is drawn.

Switch the data itself

When there are many options, putting every trace into the figure wastes memory. Instead, keep one trace and have the button replace its y values with restyle. In Plotly.js:

Dropdown switches data, buttons switch scale (Plotly.js)

args: [{ y: [data[k]] }] wraps the array in another array because restyle takes one value per trace. A very common beginner error is passing the bare array, which Plotly interprets as one value per trace and then fails. Notice also how two menus coexist: one dropdown for the data and a pair of buttons for the scale. Each menu keeps its own active choice.

Buttons that change chart type or style

You can restyle any trace property, including type:

Python
buttons = [
    dict(label="Bars", method="restyle", args=[{"type": "bar"}]),
    dict(label="Line", method="restyle", args=[{"type": "scatter", "mode": "lines"}]),
    dict(label="Area", method="restyle", args=[{"type": "scatter", "fill": "tozeroy"}]),
]

Other typical buttons: switch a colorscale ({"colorscale": "Viridis"} on a heatmap), hide all annotations (relayout with {"annotations": []}), reset the camera on a 3D chart, or switch map styles.

Placement, state and a reset button

  • active: index of the initially selected entry (-1 for none, useful for one-shot action buttons).
  • showactive: highlight the selected button.
  • pad, bgcolor, bordercolor, font: appearance.
  • With direction="up" or "left" a dropdown can open in the other direction, which helps near the bottom edge.

Menus do not know the current state of the chart. If a user zooms and then presses a button that sets yaxis.autorange, the button and the chart can disagree about which view is active. Design buttons as absolute choices ("show Profit", "log scale") rather than toggles, and your controls stay consistent.

updatemenus live in the figure JSON, so they work in standalone HTML but not in a Dash app's server-side logic. If a choice must query a database, use a Dash dropdown (dcc.Dropdown) and a callback instead. See Dash callbacks and live graphs.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which method changes both trace properties and layout properties in one click?

    update takes two dictionaries: trace changes and layout changes.
  2. 2. To show only the second of three traces, the visible list should be:

    visible takes one boolean per trace in the same order as the traces.
  3. 3. Why is the new y array wrapped in another array in args: [{ y: [data] }]?

    Restyle properties like y are given per trace. For one trace, put the array inside a list.
  4. 4. When should you prefer a Dash dropdown to updatemenus?

    updatemenus only rearrange data that already sits in the figure. Server-driven choices need a callback.

Summary

  • updatemenus add dropdowns and button rows inside the figure, with no server needed.
  • The four methods are restyle (traces), relayout (layout), update (both) and animate.
  • Switch series with visible lists, or swap the data with restyle and per-trace arrays.
  • Make buttons absolute choices, and place menus with x, y, direction and active.
  • Use Dash components when a choice must reach a server.

Next: Sliders and Range Selectors