Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách tải file .ttf trong Matplotlib bằng mpl.rcParams?

Để tải file font .ttf trong Matplotlib dùng mpl.rcParams:

  • Thiết lập kích thước hình và điều chỉnh khoảng đệm subplot.
  • Khởi tạo đường dẫn file .ttf.
  • Tạo instance FontProperties để lưu trữ và thao tác thuộc tính font.
  • Đặt font family bằng tên font phù hợp nhất với thuộc tính.
  • Tạo figure và tập subplots.
  • Đặt tiêu đề figure.
  • Hiển thị hình bằng show().

Ví dụ

from matplotlib import pyplot as plt, font_manager as fm
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
path = '/usr/share/fonts/truetype/malayalam/Karumbi.ttf'
fprop = fm.FontProperties(fname=path)
plt.rcParams['font.family'] = fprop.get_name()
fig, ax = plt.subplots()
ax.set_title('This is my title!', size=40)
plt.show()

Kết quả

Cách tải file .ttf trong Matplotlib bằng mpl.rcParams?