在这里存在一个问题,即试图绘制两个均由一个滑块控制的子图。 这应该很简单,但是我看不到让它正常工作。 我一直在关注:https://plot.ly/python/sliders/ 和许多其他帖子,但似乎仍然有问题。
我在两个子图的每个图上都使用了两个df,并尝试通过滑动条中的步长更改来更改哪一行可见。
这就是我要去的地方。第一次使用plotly,因此任何帮助和建议将不胜感激。
谢谢。
import plotly
import plotly.graph_objects as go
import numpy as np
import pandas as pd
import plotly.express 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,'C':[110,110,140,115,120],'D':[140,160,130],'E':[150,130,150]})
df2 = pd.DataFrame({'A':[140,150,'B':[150,'C':[120,'D':[170,125,150],'E':[140,180,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)):
fig.append_trace(
go.Scatter(
visible=False,line=dict(color="#00CED1",width=2),name="Time = " + str(step),x=df.columns[0:],y=df.loc[step]),row=1,col=1)
#for step in range(len(df2.index)): Tried this does not work
fig.append_trace(
go.Scatter(
visible=False,line=dict(color="red",x=df2.columns[0:],y=df2.loc[step]),row=2,col=1)
fig.data[1].visible = True
# Create and add slider
steps = []
for i in range(len(df.index)):
step = dict(method="restyle",args=["visible",[False] * len(fig.data)],)
step["args"][1][i] = True
#step["args"][1][i+1] = True # This shows them both but still not correct (different row numbers)
steps.append(step)
sliders = [dict(
active=0,currentvalue={"prefix": "Time: "},pad={"t": 50},steps=steps
)]
fig.update_yaxes(title_text="Temperature",range=[-160,260],nticks=30,col=1)
fig.update_yaxes(title_text="Pressure",range=[-169,col=1)
fig.update_layout(sliders=sliders,title="Time Series - Interactive",template ="plotly_white")
fig.update_layout(width=800,height=600,)
fig.show()