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

使用Plotly make_subplots组合子图时如何保留子图原有样式?

Plotly 子图网格布局保留原样式方法

单个带误差棒的柱状图示例代码

以下是Plotly官网的误差棒示例代码:

import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Bar(
    name='Control',
    x=['Trial 1', 'Trial 2', 'Trial 3'], y=[3, 6, 4],
    error_y=dict(type='data', array=[1, 0.5, 1.5])
))
fig.add_trace(go.Bar(
    name='Experimental',
    x=['Trial 1', 'Trial 2', 'Trial 3'], y=[4, 7, 3],
    error_y=dict(type='data', array=[0.5, 1, 2])
))
fig.update_layout(barmode='group')
fig2 = fig
fig.show()

尝试组合为网格布局的代码

以下是尝试将多个上述图表组合到一个网格布局中的代码:

# Create a grid of subplots
i_s_num_subs = 8
obj_s_fig_mv_all = make_subplots(rows=(i_s_num_subs-1) // 2 + 1, cols=2)
# Add a bar plot to each subplot
for i_s_plot in range(i_s_num_subs):
    i_s_row = i_s_plot // 2 + 1
    i_s_col = i_s_plot % 2 + 1
    i_s_num_phases = 4
    obj_s_fig_mv_tc = make_subplots(rows=1, cols=i_s_num_phases)
    i_s_sub_col = 0
    for i_s_sub_col in range(i_s_num_phases):
        obj_s_fig_mv_tc.add_trace(
            go.Bar(
                name='Control',
                x=['Trial 1', 'Trial 2', 'Trial 3'], y=[3, 6, 4],
                error_y=dict(type='data', array=[1, 0.5, 1.5])
            ),
        row=1,
        col=i_s_sub_col+1
    )
    obj_s_fig_mv_tc.update_layout(barmode='group')
    obj_s_fig_mv_all.add_trace(obj_s_fig_mv_tc.data[0], row=i_s_row, col=i_s_col)
obj_s_fig_mv_all.show()

问题

组合后的图表与单个子图样式差异明显,推测因仅将子图的单个trace添加到外层图表所致。请问是否可保留子图原有外观,仅通过外层图表实现网格排列,类似Plotly Express的分面功能?


解决方案

当然可以,核心思路是避免嵌套创建子图,直接在顶层网格中添加完整的trace集合并同步布局配置,或者直接使用Plotly Express的分面功能。

方法一:基于Graph Objects的修正实现

直接在顶层子图网格中为每个位置添加完整的双柱状带误差棒内容,同步设置布局属性:

import plotly.graph_objects as go
from plotly.subplots import make_subplots

# 创建顶层子图网格:4行2列,共8个子图
total_plots = 8
rows = (total_plots - 1) // 2 + 1
cols = 2
fig_all = make_subplots(rows=rows, cols=cols)

# 为每个子图位置添加完整的两组柱状图
for plot_idx in range(total_plots):
    row = plot_idx // 2 + 1
    col = plot_idx % 2 + 1
    
    # 添加Control组柱状图
    fig_all.add_trace(
        go.Bar(
            name='Control',
            x=['Trial 1', 'Trial 2', 'Trial 3'], 
            y=[3, 6, 4],
            error_y=dict(type='data', array=[1, 0.5, 1.5])
        ),
        row=row, col=col
    )
    # 添加Experimental组柱状图
    fig_all.add_trace(
        go.Bar(
            name='Experimental',
            x=['Trial 1', 'Trial 2', 'Trial 3'], 
            y=[4, 7, 3],
            error_y=dict(type='data', array=[0.5, 1, 2])
        ),
        row=row, col=col
    )
    
    # 为当前子图设置轴标签
    fig_all.update_xaxes(title_text=f"子图 {plot_idx+1}", row=row, col=col)
    fig_all.update_yaxes(title_text="数值", row=row, col=col)

# 全局设置分组柱状模式,调整整体尺寸
fig_all.update_layout(barmode='group', height=1200, width=1000, title_text="带误差棒的柱状图网格")
fig_all.show()

方法二:使用Plotly Express分面功能(更简洁)

将数据整理为长格式,直接调用px.bar的分面参数自动实现网格布局:

import plotly.express as px
import pandas as pd

# 构造分面所需的长格式数据
data = []
for plot_idx in range(8):
    for trial in ['Trial 1', 'Trial 2', 'Trial 3']:
        # 添加Control组数据
        data.append({
            '子图': f"图 {plot_idx+1}",
            '试验': trial,
            '组别': 'Control',
            '数值': [3,6,4][['Trial 1','Trial 2','Trial 3'].index(trial)],
            '误差': [1,0.5,1.5][['Trial 1','Trial 2','Trial 3'].index(trial)]
        })
        # 添加Experimental组数据
        data.append({
            '子图': f"图 {plot_idx+1}",
            '试验': trial,
            '组别': 'Experimental',
            '数值': [4,7,3][['Trial 1','Trial 2','Trial 3'].index(trial)],
            '误差': [0.5,1,2][['Trial 1','Trial 2','Trial 3'].index(trial)]
        })

df = pd.DataFrame(data)

# 使用Plotly Express分面功能自动生成网格
fig = px.bar(df, x='试验', y='数值', color='组别', 
             error_y='误差', barmode='group',
             facet_col='子图', facet_col_wrap=2,
             height=1200, width=1000)
fig.show()

关键说明

  • 方法一避免了嵌套make_subplots的问题,直接在顶层子图中添加所有必要trace,保证每个子图保留原有的双柱状分组样式。
  • 方法二利用Plotly Express的分面逻辑,自动处理网格布局和样式继承,代码更简洁易维护,完全匹配需求中的"类似分面功能"的要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 06:54:56