我正在寻找一种简单的方法来获取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)
这使我感到
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
我可以看出在这台机器上,matplotlib.pyplot使用DejaVu Sans。 不过,我认为应该有更简单的方法来获取这些信息。
编辑:
警告可以直接通过以下方式触发:
matplotlib.font_manager.findfont(matplotlib.font_manager.FontProperties(family=font))
Arial
字体。findfont将返回最接近给定字体属性对象的字体文件。 - alodimatplotlib.rcParams ['font.family']
- tamasgalfont_manager
用于确定绘图所使用的字体...对我来说至少它正常工作。 - tamasgal