ปรับขนาดตัวอักษรในตาราง Matplotlib
ใช้เมธอด set_fontsize() เพื่อเปลี่ยนขนาดฟอนต์ของตาราง
ขั้นตอน
- สร้าง figure และ subplots (nrows=1, ncols=1)
- สร้างข้อมูลสุ่มด้วย NumPy
- กำหนดค่า columns
- ตั้งค่า axis เป็น tight และ off
- กำหนดตัวแปร fontsize
- ตั้งค่าขนาดฟอนต์ตารางด้วย set_fontsize()
- แสดงด้วย 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()
ผลลัพธ์
