参见这里的例子:
http://matplotlib.sourceforge.net/examples/api/font_file.html
一般来说,如果你想使用一个特定的.ttf文件,你可以这样做。 (请记住,指向特定的字体文件通常是一个坏主意!)
import matplotlib.font_manager as fm
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(10))
prop = fm.FontProperties(fname='/usr/share/fonts/truetype/groovygh.ttf')
ax.set_title('This is some random font', fontproperties=prop, size=32)
plt.show()
通常,你只需要指向字体的名称,并让matplotlib担心找到特定的文件。例如。
import matplotlib.pyplot as plt
plt.plot(range(10))
plt.title('This is some random font', family='GroovyGhosties', size=32)
plt.show()
如果你想使matplotlib总是使用一个特定的字体,那么customize your .matplotlibrc file.(font.family是你想要设置的。请注意,你应该指定字体的名称,而不是指向特定.ttf文件的路径。)
作为动态执行此操作的示例(即不设置特定的.matplotlibrc文件):
import matplotlib as mpl
mpl.rcParams['font.family'] = 'GroovyGhosties'
import matplotlib.pyplot as plt
plt.plot(range(10))
plt.title('Everything is crazy!!!', size=32)
plt.show()