Find out which matplotlib font uses

I need a good way to get the default font name used by matplotlib.pyplot. The documentation indicates that the font is selected from the list in rcParams ['font.family'], which is ordered from top to bottom by priority. My first attempt 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 say that on this DejaVu Sans machine, matplotlib.pyplot is used. However, I thought that there should be an easier way to get this information.

Edit:

A warning can be triggered directly through

 matplotlib.font_manager.findfont(matplotlib.font_manager.FontProperties(family=font)) 
+6
source share
1 answer

To get a font family:

 matplotlib.rcParams['font.family'] 

If this is a common font family, such as "sans-serif", use fontfind to find the actual font:

 >>> from matplotlib.font_manager import findfont, FontProperties >>> font = findfont(FontProperties(family=['sans-serif'])) >>> font '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/mpl-data/fonts/ttf/Vera.ttf' 

I found this in font_manager unit tests: https://github.com/matplotlib/matplotlib/blob/4314d447dfc7127daa80fa295c9bd56cf07faf01/lib/matplotlib/tests/test_font_manager.py

+6
source

Source: https://habr.com/ru/post/980648/


All Articles