在Python中使用共享滑块绘制子图

问题描述 投票:0回答:1

我正在尝试绘制 3 个子图(一个在另一个下的单独图),它们共享相同的 x 范围数据,并使用滑块控制底部的所有子图。 我已经成功地用一个图做到了这一点,但我不知道如何添加其他 2 个子图。我正在与数据系列的一小部分共享代码。

from plotly.subplots import make_subplots
import plotly.express as px
import plotly.graph_objects as go
x=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # pandas series, string
y1=[90, 92, 89, 82, 82, 78, 76, 82, 85, 88] # pandas series, numpy int64
y2=[20, 21, 19, 20, 18, 17, 14, 16, 18, 23] # pandas series, numpy int64
y3=[40, 42, 41, 42, 44, 45, 47, 49, 45, 46] # pandas series, numpy int64


fig1 = make_subplots(rows=3, cols=1, shared_xaxes=True, vertical_spacing=0.1)
fig2 = px.scatter(x=x,y=y1,labels = dict(x = "time",y = "var"))
fig1 = go.Figure(data = fig2.data) # this is defined like that so I can add a second dataset on the same plot if needed, but I need y2,y3 on a separate plot
fig1.update_layout(yaxis_title='var',xaxis_title='time')
fig1.update_xaxes(rangeslider_visible=True)
fig1.show()

谢谢!

python slider axis subplot shared
1个回答
0
投票

您想要使用单个范围滑块控制三个子输出的预期图表吗?如果是这样,您可以在图形对象中添加每个图形,并在底部显示范围滑块,您可以控制它,因为 x 轴是共享的。

fig = make_subplots(rows=3, cols=1,
                    shared_xaxes=True,
                    vertical_spacing=0.1)

fig.add_trace(go.Scatter(x=x, y=y1,), row=1, col=1)
fig.add_trace(go.Scatter(x=x, y=y1,), row=2, col=1)
fig.add_trace(go.Scatter(x=x, y=y3,), row=3, col=1)

fig.update_xaxes(rangeslider_visible=False, row=1, col=1)
fig.update_xaxes(rangeslider_visible=False, row=2, col=1)
fig.update_xaxes(rangeslider_visible=True, row=3, col=1)
fig.show()

© www.soinside.com 2019 - 2024. All rights reserved.