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

Plotly Table columnwidth属性无法加宽列的问题求助

解决Plotly go.Table列宽设置后内容截断的问题

针对你遇到的columnwidth设置后列仍被截断的问题,可以试试以下几个实用方案:

  • 确保图表整体宽度足够容纳所有列宽
    Plotly图表默认会自适应容器,可能压缩你设置的列宽。直接指定图表总宽度,把列宽总和加上预留边距值作为图表宽度:

    total_width = sum(column_widths) + 100  # 加100是预留左右边距空间
    fig.update_layout(width=total_width, autosize=False)
    
  • 关闭自动换行或调整文本展示规则
    单元格内容自动换行会让列宽设置看起来失效,你可以强制关闭换行,或者调整行高适配内容:

    cells=dict(values=df.transpose().values.tolist(),
               fill_color='white',
               align='left',
               textwrap='none',  # 禁止自动换行
               line_height=1.5)  # 调整行高避免内容挤压
    
  • 调整字体大小避免内容溢出
    表头或单元格字体过大也会导致内容被截断,适当缩小字体尺寸:

    header=dict(values=list(df.columns),
                fill_color='lightgray',
                align='center',
                font=dict(size=12)),  # 调整表头字体大小
    cells=dict(values=df.transpose().values.tolist(),
               fill_color='white',
               align='left',
               font=dict(size=11))  # 调整单元格字体大小
    
  • 减少布局边距
    默认布局边距会占用部分宽度,缩小边距能给表格更多展示空间:

    fig.update_layout(margin=dict(l=20, r=20, t=20, b=20))
    

修改后的完整代码示例

column_widths = [75, 75, 75, 100, 100, 100,
                 75, 75, 75, 75, 75, 75, 75, 100, 225]

fig = pgo.Figure(data=[pgo.Table(
                columnwidth=column_widths,
                header=dict(values=list(df.columns),
                            fill_color='lightgray',
                            align='center',
                            font=dict(size=12)),
                cells=dict(values=df.transpose().values.tolist(),
                           fill_color='white',
                           align='left',
                           textwrap='none',
                           line_height=1.5,
                           font=dict(size=11)))
            ])

# 配置图表布局参数
total_width = sum(column_widths) + 100
fig.update_layout(
    width=total_width,
    autosize=False,
    margin=dict(l=20, r=20, t=20, b=20)
)

fig.show()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 20:23:22