Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

Plotly two subplots controlled by one slider

Hey Guys,

Can you have a quick check at what I am doing wrong here. I am attempting two have two subplots controlled by the same slider with data from two different dataframes. I am just trying to change the df row value with the slider but can’t seem to get this working. I have had a look at some of the other questions about this topic and cant seem to get it working.

Any help will be much appreciated. Thanks

import plotly
import plotly.graph_objects as go
import numpy as np
import pandas as pd
import as px
from plotly.subplots import make_subplots
import chart_studio.plotly as py

df = pd.DataFrame({'A':[100, 120, 100, 105, 110], 
                'B':[130, 120, 100, 105, 110],
                'C':[110, 110, 140, 115, 120],
                'D':[140, 120, 160, 120, 130],                   
                'E':[150, 130, 100, 105, 150]})
df2 = pd.DataFrame({'A':[140, 150, 110, 115, 120], 
                'B':[150, 120, 100, 105, 110],
                'C':[120, 120, 110, 115, 120],
                'D':[170, 140, 120, 125, 150],                   
                'E':[140, 180, 115, 115, 140]})

fig = make_subplots(rows=2, cols=1,shared_xaxes=True)

# Add traces, one for each slider step
for step in range(len(df.index)):
            line=dict(color="#00CED1", width=2),
            name="Time = " + str(step),
            y=df.loc[step]),row=1, col=1)
#for step in range(len(df2.index)):# Tried this does not work
            line=dict(color="red", width=2),
            name="Time = " + str(step),
            y=df2.loc[step]),row=2, col=1)[1].visible = True

# Create and add slider
steps = []
for i in range(len(df.index)):
    step = dict(method="restyle",
        args=["visible", [False] * len(],
    step["args"][1][i] = True
    #step["args"][1][i+1] = True # This shows them both but still not correct(Showing wrong traces 1,2)

sliders = [dict(
    currentvalue={"prefix": "Time:  "},
    pad={"t": 50},

fig.update_yaxes(title_text="Temperature", range=[-160, 260],nticks=30, row=1, col=1)
fig.update_yaxes(title_text="Pressure", range=[-169, 260],nticks=30, row=2, col=1)
fig.update_layout(sliders=sliders, title="Time Series - Interactive", template ="plotly_white")


@birkett0011 Replace the line after
steps =[]

for i in range(len(

because index goes from 0 to 4, while data from 0 to 9, and you want to plot the trace (data) corresponding to each row in df1, respectively, df2.

1 Like

Hey mate thanks for your response.

Do you have any idea on how to make it show both plots at the right index at the same time.

Currently it alternates between showing them. Shows the top one then next step displays the bottom one and so on.

I am looking for them to both be displayed at the same time at the same index all controlled by the slider.


@birkett0011 To display simultaneously the traces from the two subplots, at each step, you should define the steps as follows:

steps = []

for i in range(0, len(, 2):
    step = dict(
        args=["visible", [False] * len(],
    step["args"][1][i:i+2] = [True, True]

Awesome, Thanks again

1 Like