I've searched for a while, trying to use Chinese font in my matplotlib histogram. Instead of installing the font locally, I tried to follow the post to use the raw TTF file from Github. However, I got In FT2Font: Can not load face. Unknown file format.
message. Here is my code:
from tempfile import NamedTemporaryFile
import urllib3
import matplotlib.font_manager as fm
import matplotlib.pyplot as plt
https = urllib3.PoolManager()
github_url = 'https://github.com/google/fonts/tree/main/ofl/notosanssc/NotoSansSC[wght].ttf'
url = github_url + '?raw=true' # will trigger the download
response = https.request('GET', url)
# print(response.data) # to confirm the successful response with data
f = NamedTemporaryFile(delete=False, suffix='.ttf')
f.write(response.data)
f.close()
f.name # C:\\Users\\<username>\\AppData\\Local\\Temp\\tmp9i1czwm1.ttf'
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()
Reinstalling matplotlib and removing cache gave me no luck. What could be the issue? Thanks.