Bạn có thể nhận .ttf tập tin từ repo google 'phông chữ' trên github. Bạn có thể chọn một phông chữ từ danh sách ở đó và tìm liên kết đến tệp .ttf. Ví dụ: nếu bạn đi vào thư mục 'giống nhau', bạn sẽ tìm thấy tệp có tên 'Alike-Regular.ttf', có URL là: https://github.com/google/fonts/blob/master/ofl/alike/Alike-Regular.ttf.
Khi bạn tìm phông chữ của bạn, bạn có thể sử dụng đoạn mã sau để tải nó vào matplotlib "on the fly", sử dụng một tập tin tạm thời:
from tempfile import NamedTemporaryFile
import urllib2
import matplotlib.font_manager as fm
import matplotlib.pyplot as plt
github_url = 'https://github.com/google/fonts/blob/master/ofl/alike/Alike-Regular.ttf'
url = github_url + '?raw=true' # You want the actual file, not some html
response = urllib2.urlopen(url)
f = NamedTemporaryFile(delete=False, suffix='.ttf')
f.write(response.read())
f.close()
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
prop = fm.FontProperties(fname=f.name)
ax.set_title('this is a special font:\n%s' % github_url, fontproperties=prop)
ax.set_xlabel('This is the default font')
plt.show()
Kết quả:
https : //stackoverflow.com/questions/7726852/how-to-use-a-random-otf-or-ttf-font-in-matplotlib – Oz123
Bạn có ý nghĩa gì khi "đang di chuyển"? Bạn có muốn bỏ qua việc tải xuống và tải trực tiếp từ Google không? – Shovalt