如何利用 matplotlib.pyplot 更改表格的字號?


要使用 matplotlib 更改表格的字型大小,我們可以使用 set_fontsize() 方法。

步驟

  • 建立一個圖形和一組子圖,nrows=1ncols=1
  • 使用 numpy 建立隨機資料。
  • 建立 columns 引數。
  • 讓座標軸 tightoff
  • 初始化變數 fontsize 以更改字型大小。
  • 使用 set_font_size() 方法設定表格的字型大小。
  • 要顯示圖形,使用 show() 方法。

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 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')
the_table = axs.table(cellText=data, colLabels=columns, loc='center')
the_table.auto_set_font_size(False)
the_table.set_fontsize(10)
plt.show()

輸出

更新於: 15-05-2021

7000+ 瀏覽量

開啟你的 職業生涯

完成課程獲取認證

立即開始
廣告
© . All rights reserved.