你可以通过设置单元格高度和整个图像高度来实现:
import plotly.graph_objects as go
CELL_HEIGHT = 30
a_scores = [100, 90, 80, 90]
b_scores = [95, 85, 75, 95]
layout = go.Layout(
autosize=True,
margin={'l': 0, 'r': 0, 't': 0, 'b': 0},
height=CELL_HEIGHT * (len(a_scores) + 1)
)
fig = go.Figure(
data=[
go.Table(
header=dict(values=['A Scores', 'B Scores']),
cells=dict(values=[a_scores, b_scores],
height=CELL_HEIGHT)
)
],
layout=layout
)
fig.write_image("fig1.png", scale=5)
![完美裁剪的4行和标题表格](https://istack.dev59.com/2s2rn.webp)
如果您想为标题设置不同的高度:
import plotly.graph_objects as go
CELL_HEIGHT = 30
HEADER_CELL_HEIGHT = 100
a_scores = [100, 90, 80, 90]
b_scores = [95, 85, 75, 95]
layout = go.Layout(
autosize=True,
margin={'l': 0, 'r': 0, 't': 0, 'b': 0},
height=CELL_HEIGHT * len(a_scores) + HEADER_CELL_HEIGHT
)
fig = go.Figure(
data=[
go.Table(
header=dict(values=['A Scores', 'B Scores'], height=HEADER_CELL_HEIGHT),
cells=dict(values=[a_scores, b_scores],
height=CELL_HEIGHT)
)
],
layout=layout
)
fig.write_image("fig1.png", scale=5)
![4行和不同大小表头的表格完美裁剪](https://istack.dev59.com/PO2zf.webp)
我尝试了不同的数据大小、表头单元格大小和单元格大小,它总是完美地裁剪。