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

Matplotlib自适应布局时如何增加表格单元格上下内边距

解决Matplotlib表格单元格上下内边距问题

你当前的表格尺寸、字号都合适,仅单元格上下空间拥挤,可通过以下两种方式增加上下内边距:

  • 方法一:为所有单元格设置统一内边距
    Matplotlib表格的单元格对象支持set_pad()方法,遍历所有单元格即可设置内边距,同时需调整画布高度适配修改后的表格:

    columns=['test', 'test', 'test', 'test', 'test', 'test', 'test'] 
    rows=['test', 'test', 'test', 'test']
    data=[['12.103700223192147', '125.7421234247654', '2833.324510310368', '3.3228806335297043', '103.23943179349797', '3.5559560514887627', '3.531198576041594'], ['312.22548840603355', '335.1632073701661', '688.6576628644414', '2.0598429919314976', '18.362768351047666', '1.3152545139887966', '1.3155809229679902'], ['461.0540288130374', '484.64756706242093', '757.5346438101953', '5.1333110653922684', '15.039352280544852', '1.5898237491783822', '1.5902652486082376'], ['263.82124695572776', '313.264919044118', '276.03008066454083', '140.61329994689453', '7.034269824508681', '6.71122466853062', '4.613448380627191']]
    
    wpad,hpad=0.1,0.1
    # 增加画布高度,适配内边距调整后的表格
    fig = plt.figure(figsize=(len(columns)*3+wpad, len(rows)*3.5+hpad))
    tab=plt.table(data,rowLabels=rows,colLabels=columns,cellLoc='center',loc='top')
    tab.auto_set_font_size(False)
    tab.auto_set_column_width(col=list(range(len(columns))))
    
    # 遍历所有单元格,设置上下左右内边距(0.1对应约0.1英寸的视觉效果)
    for cell in tab.get_celld().values():
        cell.set_pad(0.1)
    
    plt.axis('off')
    plt.show()
    

    注:set_pad()会同时调整单元格的上下左右内边距,若需单独控制上下空间,可使用方法二。

  • 方法二:单独调整行高实现上下空间扩展
    若只想增加上下方向的空间,可直接修改每行的高度,同样需要调整画布高度以完整显示表格:

    columns=['test', 'test', 'test', 'test', 'test', 'test', 'test'] 
    rows=['test', 'test', 'test', 'test']
    data=[['12.103700223192147', '125.7421234247654', '2833.324510310368', '3.3228806335297043', '103.23943179349797', '3.5559560514887627', '3.531198576041594'], ['312.22548840603355', '335.1632073701661', '688.6576628644414', '2.0598429919314976', '18.362768351047666', '1.3152545139887966', '1.3155809229679902'], ['461.0540288130374', '484.64756706242093', '757.5346438101953', '5.1333110653922684', '15.039352280544852', '1.5898237491783822', '1.5902652486082376'], ['263.82124695572776', '313.264919044118', '276.03008066454083', '140.61329994689453', '7.034269824508681', '6.71122466853062', '4.613448380627191']]
    
    wpad,hpad=0.1,0.1
    # 调整画布高度,适配行高增加后的表格
    fig = plt.figure(figsize=(len(columns)*3+wpad, len(rows)*3.5+hpad))
    tab=plt.table(data,rowLabels=rows,colLabels=columns,cellLoc='center',loc='top')
    tab.auto_set_font_size(False)
    tab.auto_set_column_width(col=list(range(len(columns))))
    
    # 遍历所有行(包含表头行),设置统一行高
    total_rows = len(rows) + 1
    target_height = 0.12  # 默认行高约0.08,数值越大上下空间越充足
    for i in range(total_rows):
        for j in range(len(columns)):
            tab._cells[(i, j)].set_height(target_height)
    
    plt.axis('off')
    plt.show()
    

    注:target_height可根据需求调整,数值越大,单元格上下空间越宽松。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 00:43:12