Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีเปลี่ยนขนาดฟอนต์ตารางใน Matplotlib.pyplot


ปรับขนาดตัวอักษรในตาราง Matplotlib

ใช้เมธอด set_fontsize() เพื่อเปลี่ยนขนาดฟอนต์ของตาราง

ขั้นตอน

  1. สร้าง figure และ subplots (nrows=1, ncols=1)
  2. สร้างข้อมูลสุ่มด้วย NumPy
  3. กำหนดค่า columns
  4. ตั้งค่า axis เป็น tight และ off
  5. กำหนดตัวแปร fontsize
  6. ตั้งค่าขนาดฟอนต์ตารางด้วย set_fontsize()
  7. แสดงด้วย 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()

ผลลัพธ์

วิธีเปลี่ยนขนาดฟอนต์ตารางใน Matplotlib.pyplot