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

如何在Plotly绘图(非Plotly Dash)中添加多个下拉菜单并调整图幅

实现多下拉菜单控制Plotly图表(含尺寸调整)

下面是在你原有代码基础上,新增图表尺寸调整下拉菜单的完整实现,同时保留原有的子图显示控制菜单:

import plotly.graph_objs as go
from plotly import subplots 

# 定义原始轨迹
trace0 = go.Scatter(x=[1, 2, 3], y=[4, 5, 6], mode="lines+markers")
trace1 = go.Scatter(x=[1, 2, 3], y=[5, 4, 6], mode="lines+markers")
trace2 = go.Scatter(x=[1, 2, 3], y=[6, 5, 4], mode="lines+markers")

# 创建3行1列的子图
fig = subplots.make_subplots(rows=3, cols=1, shared_xaxes=True, horizontal_spacing=0.5)
fig.add_trace(trace0, 1, 1)
fig.add_trace(trace1, 1, 1)
fig.add_trace(trace2, 1, 1)
fig.add_trace(trace0, 2, 1)
fig.add_trace(trace0, 3, 1)

# 定义两个下拉菜单:子图显示控制 + 尺寸调整
update_menus = [
    # 第一个菜单:控制子图显示状态
    go.layout.Updatemenu(
        active=0,
        x=0,
        y=1.1,
        direction="down",
        buttons=[
            dict(label='全部显示',
                 method='update',
                 args=[{'visible': [True, True, True, True, True]},
                       {'title': '全部子图', 'showlegend': True}]),
            dict(label='仅显示第一组',
                 method='update',
                 args=[{'visible': [True, False, False, True, True]},
                       {'title': '仅第一组', 'showlegend': True}]),
            dict(label='仅显示第二组',
                 method='update',
                 args=[{'visible': [False, True, False, True, True]},
                       {'title': '仅第二组', 'showlegend': True}]),
            dict(label='仅显示第三组',
                 method='update',
                 args=[{'visible': [False, False, True, False, False]},
                       {'title': '仅第三组', 'showlegend': True}]),
        ]
    ),
    # 第二个菜单:调整图表尺寸
    go.layout.Updatemenu(
        active=0,
        x=0.2,
        y=1.1,
        direction="down",
        buttons=[
            dict(label='默认尺寸',
                 method='update',
                 args=[{}, {'height': 600, 'width': 800, 'title': f'默认尺寸 (800x600)'}]),
            dict(label='高窄模式',
                 method='update',
                 args=[{}, {'height': 800, 'width': 600, 'title': f'高窄模式 (600x800)'}]),
            dict(label='矮宽模式',
                 method='update',
                 args=[{}, {'height': 400, 'width': 1000, 'title': f'矮宽模式 (1000x400)'}]),
            dict(label='超大尺寸',
                 method='update',
                 args=[{}, {'height': 1000, 'width': 1200, 'title': f'超大尺寸 (1200x1000)'}]),
        ]
    )
]

# 应用菜单配置,并设置初始尺寸
fig.update_layout(
    updatemenus=update_menus,
    height=600,
    width=800,
    title='多下拉菜单控制图表'
)

fig.show()

关键说明

  • 多菜单配置:update_menus是一个列表,每个元素对应一个独立的下拉菜单,通过x、y参数调整菜单位置,避免重叠。
  • 尺寸调整逻辑:尺寸菜单的按钮使用method='update',通过args的第二个元素修改layout的height和width属性,第一个元素留空(不需要修改轨迹可见性)。
  • 菜单样式:direction="down"设置菜单展开方向,active参数指定默认选中的按钮索引。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 09:10:52