减小表格子图之间的大小

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

我正在尝试将两个表添加到报告中,但表之间的空间存在问题。通过 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()

这将创建以下情节:

我尝试过的事情:

  1. 调整
    height
    中的
    fig.update_layout
    但小于 850 会切断第二个表的底部
  2. vertical_spacing
    中的
    make_sub_plots
    参数不会变为负数
  3. 我无法为
    rowspan
     中的 
    specs
     参数设置不同的 
    make_sub_plots

如何缩小桌子之间的间隙?

python plotly
1个回答
0
投票

您可以尝试对 make_subplots

update_layout
 进行这两个 
调整
:

    fig = make_subplots(
        rows=2,
        cols=1,
        vertical_spacing=0,
        subplot_titles=("Overall summary", "Year"),
        specs=[
            [{"type": "table"}],
            [{"type": "table"}],
        ],  # add the parameter below
            row_heights=[0.2, 0.8]  # <<< 20% (1st ta) against 80% (2nd ta)
    )
    fig.update_layout(
        width=500,
        height=550, # << I decrease it
        title_text="Report",
        margin=dict(b=0) # << and added this one
    )

输出:

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