Black Lives Matter. Please consider donating to Black Girls Code today.

Using one slider to control multiple subplots (NOT multiple traces)

I have been making slider charts such as the one here: https://plot.ly/python/sliders/. However, how can create a slider that controls multiple subplots? Note that I’m trying to control multiple traces (therefore, the link here doesn’t help). What I want is to add a slider to a set of subplots, such as the content of all the subplots move as I move the slider.

Is that doable in Plotly through the Python API?

Hi @carpenter,

This isn’t impossible, but it would require a bit of bookkeeping. Essentially you would need to keep track of the order in which you add the traces to the figure subplots, and then duplicate the # Toggle i'th trace to "visible" line once for the trace that you want to be visible in each subplot.

-Jon

Thank you. Can you give me a quick example? I’m a bit confused as to how to proceed.

Hi @carpenter,

Here’s an example with two subplots, and 3 traces per subplot

import plotly.graph_objs as go
from plotly.tools import make_subplots

fig = make_subplots(1, 2)

fig.add_scatter(y=[1, 3, 2], row=1, col=1, visible=True)
fig.add_scatter(y=[3, 1, 1.5], row=1, col=1, visible='legendonly')
fig.add_scatter(y=[2, 2, 1], row=1, col=1, visible='legendonly')
fig.add_scatter(y=[1, 3, 2], row=1, col=2, visible=True)
fig.add_scatter(y=[1.5, 2, 2.5], row=1, col=2, visible='legendonly')
fig.add_scatter(y=[2.5, 1.2, 2.9], row=1, col=2, visible='legendonly')

steps = []
for i in range(3):
    step = dict(
        method = 'restyle',  
        args = ['visible', ['legendonly'] * len(fig.data)],
    )
    step['args'][1][i] = True
    step['args'][1][i+3] = True
    steps.append(step)

sliders = [dict(
    steps = steps,
)]

fig.layout.sliders = sliders

go.FigureWidget(fig)

Hope that helps!
-Jon

2 Likes

Thank you. Very helpful!