如何将数字与单元格顶部对齐?

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

我想将数字与每个单元格的左上角对齐。我能够找到

set_text_props
,但它没有按照我的预期进行对齐。

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot()
ax.axis('off')

table = ax.table(
    cellText=[[1, 2, 3, 4, 5, 6, 7],
              [8, 9, 10, 11, 12, 13, 14]],
    loc='center',
    cellLoc='left',
)

table.set_fontsize(10)
table.scale(1, 4)

for (row, col), cell in table.get_celld().items():
    cell.set_text_props(va='bottom')
python matplotlib
1个回答
0
投票

(这是一个黑客,不是解决方案)

在文本中添加换行符并调整

cell.PAD

合作实验室

enter image description here

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(4,3))
ax = fig.add_subplot()
ax.axis('off')

cellText=[[1, 2, 3, 4, 5, 6, 7],
          [8, 9, 10, 11, 12, 13, 14]]
for i in range(len(cellText)):
    for j in range(len(cellText[i])):
        cellText[i][j] = f"{cellText[i][j]}\n"

table = ax.table(
    cellText=cellText,
    loc='center',
    cellLoc='left',
)

table.set_fontsize(10)
table.scale(1, 4)

for (row, col), cell in table.get_celld().items():
    cell.set_text_props(va='bottom')
    cell.PAD=0.05
© www.soinside.com 2019 - 2024. All rights reserved.