Im looking for a nice way to get the name of the default font that is used by matplotlib.pyplot. The documentation indicates that the font is selected from the list in rcParams['font.family'] which is ordered top down by priority.
My first try was to check for warnings, i.e.,
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)
which gives me
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
I can tell that on this machine, DejaVu Sans is used by matplotlib.pyplot.
However, I thought there should be an easier way to obtain this information.
Edit:
The warning can be triggered directly via
matplotlib.font_manager.findfont(matplotlib.font_manager.FontProperties(family=font))
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…