Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 42 ยท Interactivity and Animation

Sliders and Range Selectors

Add parameter sliders, range sliders and time range selector buttons to Plotly charts: layout.sliders steps, rangeslider, rangeselector, and how they differ.

Plotly has three different "slider-like" controls, and beginners often mix them up because they look similar. Knowing which is which saves hours.

  1. layout.sliders: a parameter slider. Each position (a "step") applies a change to the figure: show year 2015, set a frequency, switch a threshold. It is a row of buttons presented as a slider.
  2. xaxis.rangeslider: a small overview chart under the main plot. Dragging its handles changes the visible x-range, like a zoom control. Common for time series.
  3. xaxis.rangeselector: a row of preset buttons ("1m", "6m", "YTD", "All") that set the x-range of a date axis.

Parameter sliders: layout.sliders

A slider has a list of steps. Each step looks exactly like an updatemenu button: a label, a method ("restyle", "relayout", "update" or "animate") and args. When the user drags the handle to a step, Plotly runs that step's method.

The classic recipe: add one trace per slider position, hide all but one, and let each step choose which trace is visible.

Python A frequency slider built with visible traces

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.

currentvalue shows the chosen label above the slider, pad moves the slider away from the plot, and len, x, y and xanchor position it. Setting transition={"duration": 300} makes the move animated.

When you have many steps, keeping every trace in the figure becomes heavy. The alternative is restyle with new data for a single trace, or Plotly.js code that generates the steps from a function. Here the steps are produced in a loop, and the slider changes y directly.

Parameter slider that rewrites y (Plotly.js)

Range sliders and range selectors for time series

For a long time series the user wants to look at the whole history and zoom into a period. Two settings on a date axis give you that:

  • fig.update_xaxes(rangeslider_visible=True) adds the overview slider.
  • rangeselector=dict(buttons=[...]) adds the preset buttons. Each button has count, label, step ("month", "year", "day", "hour", "minute", "second", "all") and stepmode ("backward" counts back from the end, "todate" starts at the period start, giving "year to date").
Python A date axis with a range slider and selector buttons

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 range slider and the selector both rewrite xaxis.range. That is why they cooperate: pressing "6m" moves the slider handles, and dragging the handles deselects the button.

Two practical notes. First, the range slider holds a copy of the full series, so with hundreds of thousands of points it slows the page; for those, consider server-side downsampling (see deploying and performance). Second, in a figure with several y-axes you can show a small chart in the slider for each with rangeslider_yaxis; by default only the first trace is drawn there.

The same buttons work in Plotly.js:

Range selector buttons in Plotly.js

Which control for which job?

flowchart TD
    A["The user should change something"] --> B{"What changes?"}
    B -->|"Which part of a time axis is visible"| C["rangeslider / rangeselector"]
    B -->|"A parameter or category, step by step"| D["layout.sliders"]
    B -->|"A choice from a list, or a toggle"| E["updatemenus (dropdown / buttons)"]
    B -->|"Time moves by itself"| F["Animation frames (next lesson)"]

Slider-like controls look alike but answer different questions.

If a slider drives a Dash app's server-side computation, use dcc.Slider or dcc.RangeSlider and a callback. The layout sliders in this lesson only reshuffle data that already sits in the browser, which makes them instant and offline-capable.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which control is a mini overview chart under the plot that sets the visible x-range?

    The range slider is the overview with two handles. The range selector is the preset buttons.
  2. 2. What does stepmode="todate" with count=1 and step="year" produce?

    todate starts at the beginning of the period (January 1), while backward counts back from the last date.
  3. 3. What does a step in layout.sliders contain?

    Each step runs a restyle, relayout, update or animate call when the handle reaches it.
  4. 4. Your slider has 200 positions. Which approach keeps the figure light?

    Hidden traces still live in the figure JSON. Replacing the data of one trace keeps the figure small.

Summary

  • layout.sliders apply parameter changes through steps (restyle, relayout, update, animate).
  • xaxis.rangeslider is the zoom overview; xaxis.rangeselector adds "1m / 6m / YTD / All" presets.
  • Use visibility toggling for a few positions and restyle of data for many.
  • All three controls run in the browser without a server; use Dash components for server work.
  • Give every slider a currentvalue label and padding so it is readable.

Next: Animations and Frames