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

如何缩小Plotly子图中两个表格间的间距?

缩小Plotly子图中两个表格的间距问题

我尝试在报告中添加两个表格,但表格间的间距存在问题。以下是可复现代码(MRE):

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots


def main():
    fig = make_subplots(
        rows=2,
        cols=1,
        vertical_spacing=0,
        subplot_titles=("Overall summary", "Year"),
        specs=[
            [{"type": "table"}],
            [{"type": "table"}],
        ],
    )

    df1 = pd.DataFrame(
        {
            "all": {0: 1},
            "n_match": {0: 1219076},
            "n_odds": {0: 228758},
            "acc": {0: 0.735},
        }
    )

    fig.add_table(
        header=dict(
            values=list(df1.columns),
            align="right",
        ),
        cells=dict(
            values=[df1[k].tolist() for k in df1.columns[0:]],
            align="right",
        ),
        row=1,
        col=1,
    )

    df2 = pd.DataFrame(
        {
            "year": {
                0: 2009,
                1: 2010,
                2: 2011,
                3: 2012,
                4: 2013,
                5: 2014,
                6: 2015,
                7: 2016,
                8: 2017,
                9: 2018,
                10: 2019,
                11: 2020,
                12: 2021,
                13: 2022,
                14: 2023,
            },
            "n_match": {
                0: 79510,
                1: 77020,
                2: 80496,
                3: 86096,
                4: 89434,
                5: 93976,
                6: 96190,
                7: 97396,
                8: 96578,
                9: 92086,
                10: 76258,
                11: 27054,
                12: 67464,
                13: 85188,
                14: 74330,
            },
            "n_odds": {
                0: 4422,
                1: 4756,
                2: 5010,
                3: 5556,
                4: 6288,
                5: 15644,
                6: 17166,
                7: 16632,
                8: 15952,
                9: 16060,
                10: 18232,
                11: 9908,
                12: 34294,
                13: 40430,
                14: 18408,
            },
            "acc": {
                0: 0.734,
                1: 0.734,
                2: 0.742,
                3: 0.743,
                4: 0.743,
                5: 0.75,
                6: 0.748,
                7: 0.747,
                8: 0.745,
                9: 0.733,
                10: 0.718,
                11: 0.707,
                12: 0.715,
                13: 0.718,
                14: 0.715,
            },
        }
    )
    
    fig.add_table(
        header=dict(
            values=list(df2.columns),
            align="right",
        ),
        cells=dict(
            values=[df2[k].tolist() for k in df2.columns[0:]],
            align="right",
        ),
        row=2,
        col=1,
    )
    
    fig.update_layout(width=500, height=850, title_text="Report")
    
    fig.show()


if __name__ == "__main__":
    main()

该代码生成的图表中两个表格间存在较大间隙。我已尝试以下方法:

  • 调整fig.update_layout中的height参数,但低于850时会截断下方表格;
  • 设置make_subplots的vertical_spacing为0,但该参数无法设为负值;
  • 尝试调整make_subplots的specs参数中的rowspan,但不可行。

请问应如何缩小两个表格间的间距?


解决方案

问题根源在于默认的子图行高是均分的,第一个表格仅1行数据却占据了一半的高度,导致表格下方出现大量空白。通过设置row_heights参数让行高匹配表格内容,即可消除多余间隙。

修改后的核心代码如下:

fig = make_subplots(
    rows=2,
    cols=1,
    vertical_spacing=0,
    subplot_titles=("Overall summary", "Year"),
    specs=[
        [{"type": "table"}],
        [{"type": "table"}],
    ],
    # 关键修改:设置行高比例,匹配表格内容量
    row_heights=[0.15, 0.85]
)

# 同时降低整体高度,避免冗余空间
fig.update_layout(width=500, height=600, title_text="Report")

完整修改后的代码:

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots


def main():
    fig = make_subplots(
        rows=2,
        cols=1,
        vertical_spacing=0,
        subplot_titles=("Overall summary", "Year"),
        specs=[
            [{"type": "table"}],
            [{"type": "table"}],
        ],
        row_heights=[0.15, 0.85]
    )

    df1 = pd.DataFrame(
        {
            "all": {0: 1},
            "n_match": {0: 1219076},
            "n_odds": {0: 228758},
            "acc": {0: 0.735},
        }
    )

    fig.add_table(
        header=dict(
            values=list(df1.columns),
            align="right",
        ),
        cells=dict(
            values=[df1[k].tolist() for k in df1.columns[0:]],
            align="right",
        ),
        row=1,
        col=1,
    )

    df2 = pd.DataFrame(
        {
            "year": {
                0: 2009,
                1: 2010,
                2: 2011,
                3: 2012,
                4: 2013,
                5: 2014,
                6: 2015,
                7: 2016,
                8: 2017,
                9: 2018,
                10: 2019,
                11: 2020,
                12: 2021,
                13: 2022,
                14: 2023,
            },
            "n_match": {
                0: 79510,
                1: 77020,
                2: 80496,
                3: 86096,
                4: 89434,
                5: 93976,
                6: 96190,
                7: 97396,
                8: 96578,
                9: 92086,
                10: 76258,
                11: 27054,
                12: 67464,
                13: 85188,
                14: 74330,
            },
            "n_odds": {
                0: 4422,
                1: 4756,
                2: 5010,
                3: 5556,
                4: 6288,
                5: 15644,
                6: 17166,
                7: 16632,
                8: 15952,
                9: 16060,
                10: 18232,
                11: 9908,
                12: 34294,
                13: 40430,
                14: 18408,
            },
            "acc": {
                0: 0.734,
                1: 0.734,
                2: 0.742,
                3: 0.743,
                4: 0.743,
                5: 0.75,
                6: 0.748,
                7: 0.747,
                8: 0.745,
                9: 0.733,
                10: 0.718,
                11: 0.707,
                12: 0.715,
                13: 0.718,
                14: 0.715,
            },
        }
    )
    
    fig.add_table(
        header=dict(
            values=list(df2.columns),
            align="right",
        ),
        cells=dict(
            values=[df2[k].tolist() for k in df2.columns[0:]],
            align="right",
        ),
        row=2,
        col=1,
    )
    
    fig.update_layout(width=500, height=600, title_text="Report")
    
    fig.show()


if __name__ == "__main__":
    main()

这样修改后,两个表格会紧密排列,同时整体高度适配内容,不会出现截断或冗余间隙。你可以根据实际需求微调row_heights的比例和height参数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 18:44:55