如何使用 matplotlib.pyplot 更改表格的字体大小?
matplotlibpythondata visualization更新于 2025/4/21 19:24:17
要使用 matplotlib 更改表格的字体大小,我们可以使用 set_fontsize() 方法。
步骤
- 创建一个图形和一组子图,nrows=1 和 ncols=1。
- 使用 numpy 创建随机数据。
- 创建 columns 值。
- 使轴 紧密 和 关闭。
- 初始化变量 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()
输出

相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

