如何更改 Matplotlib 中现有表格的比例?

matplotlibserver side programmingprogramming更新于 2025/4/30 17:07:17

要更改表格的比例,我们可以使用 scale() 方法。步骤 −

  • 创建一个图形和一组子图,nrows=1 和 ncols=1。

  • 使用 numpy 创建随机数据。

  • 使列成为值。

  • 使轴紧密并偏离。

  • 初始化变量 fontsize  以更改 fontsize。

  • 要设置表格的字体大小并缩放表格,我们可以使用 1.5 和 1.5。

  • 要显示图形,请使用 show() 方法。

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, axs = plt.subplots(1, 1)
data = np.random.random((10, 3))
columns = ("Column I", "Column II", "Column III")
axs.axis('tight')
axs.axis('off')
fontsize = 10
the_table = axs.table(cellText=data, colLabels=columns, loc='center')
the_table.set_fontsize(fontsize)
the_table.scale(1.5, 1.5)
plt.show()

输出


相关文章