You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python实现带共享滑块的3个垂直子散点图问题求助

解决3个垂直散点图共享底部滑块的问题

直接修改你的代码,保留子图结构并依次添加三个散点图,同时实现共享x轴滑块:

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]
y1=[90, 92, 89, 82, 82, 78, 76, 82, 85, 88]
y2=[20, 21, 19, 20, 18, 17, 14, 16, 18, 23]
y3=[40, 42, 41, 42, 44, 45, 47, 49, 45, 46]

# 创建3行1列的子图,开启x轴共享
fig = make_subplots(rows=3, cols=1, shared_xaxes=True, vertical_spacing=0.1)

# 生成每个散点图并添加到对应子图行
# 第一个子图(第1行)
fig1 = px.scatter(x=x, y=y1, labels={"x": "time", "y": "var1"})
fig.add_trace(fig1.data[0], row=1, col=1)

# 第二个子图(第2行)
fig2 = px.scatter(x=x, y=y2, labels={"x": "time", "y": "var2"})
fig.add_trace(fig2.data[0], row=2, col=1)

# 第三个子图(第3行)
fig3 = px.scatter(x=x, y=y3, labels={"x": "time", "y": "var3"})
fig.add_trace(fig3.data[0], row=3, col=1)

# 设置每个子图的y轴标题
fig.update_yaxes(title_text="var1", row=1, col=1)
fig.update_yaxes(title_text="var2", row=2, col=1)
fig.update_yaxes(title_text="var3", row=3, col=1)

# 设置底部x轴标题和滑块,仅在最底部子图显示滑块
fig.update_xaxes(title_text="time", row=3, col=1, rangeslider_visible=True)

# 调整整体布局
fig.update_layout(height=600, title_text="三个垂直散点图(共享x轴滑块)")
fig.show()

关键说明:

  • 不要覆盖make_subplots创建的子图对象,而是用add_trace逐个添加散点数据到对应行
  • shared_xaxes=True确保三个子图的x轴联动,拖动滑块或缩放时所有子图同步变化
  • 仅对最底部的x轴(第3行)设置rangeslider_visible=True,避免重复显示滑块
  • 可以通过vertical_spacing调整子图之间的垂直间距,让布局更美观

内容的提问来源于stack exchange,提问作者AL B

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 11:42:51