我正在寻找一种很好的方法来获取matplotlib.pyplot使用的默认字体的名称.该文档表明该字体是从rcParams ['font.family']中的列表中选择的,该列表按优先级自上而下排序.我的第一次尝试是检查警告,即,
import matplotlib.pyplot as plt
import warnings
for font in plt.rcParams['font.sans-serif']:
print font
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
plt.rcParams['font.family'] = font
plt.text(0,0,font)
plt.savefig(font+'.png')
if len(w):
print "Font {} not found".format(font)
Run Code Online (Sandbox Code Playgroud)
这给了我
Bitstream Vera Sans
Font Bitstream Vera Sans not found
DejaVu Sans
Lucida Grande
Font Lucida Grande not found
Verdana
Geneva
Font Geneva not found
Lucid
Font Lucid not found
Arial
Helvetica
Font Helvetica not found
Avant Garde
Font Avant Garde not found
sans-serif
Run Code Online (Sandbox Code Playgroud)
我可以说,在这台机器上,dejaVu Sans被matplotlib.pyplot使用.但是,我认为应该有一种更简单的方法来获取这些信息.
编辑: …