Hi guys,
I´m pretty new to working with plotly and i have a problem where you hopefully can help me.
I have two subplots and want to update the data for the y-axis of both plots with a dropdown menu. For the first plot that´s working fine. But not for the second plot.
I think I´m using the wrong key to access the second y-axis.
Here is some example code:
import pandas as pd
import plotly.express as px
from plotly.subplots import make_subplots
# Create demo data
df = pd.DataFrame(dict(A=[1, 2, 3], B=[1, 1, 1], C=[2, 2, 2], D=[3, 3, 3], E=[4, 4, 4]))
df2 = pd.DataFrame(dict(A=[1, 2, 3, 4], B=[5, 5, 5, 5], C=[6, 6, 6, 6], D=[7, 7, 7, 7], E=[8, 8, 8, 8]))
all_df = {"DataA": df, "DataB": df2}
# Create subplots
fig = make_subplots(rows=2, cols=1, vertical_spacing=0.01)
traces = px.line(
all_df["DataA"],
x="A",
y=all_df["DataA"].columns[1:3],
markers=True,
).data
fig.add_traces(traces, 1, 1)
traces = px.line(
all_df["DataA"],
x="A",
y=all_df["DataA"].columns[3:],
markers=True,
).data
fig.add_traces(traces, 2, 1)
# Add buttons
buttons = [
dict(
method="restyle",
args=[
{
"y": [all_df[n].get(u) for u in all_df[n].columns[1:3]],
"x": [all_df[n].get("A")],
"y2": [all_df[n].get(u) for u in all_df[n].columns[3:]], # I think this is wrong
},
],
label=n,
)
for n in all_df.keys()
]
fig.layout.updatemenus = [{"buttons": buttons}]
fig.show()
I hope you can help me with this.