如何在matplotlib中获取字体系列(或字体名称)列表

Par*_*jar 12 python matplotlib python-2.7

如何在matplotlib中获取字体系列(或字体名称)列表.

import matplotlib.font_manager
matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')
Run Code Online (Sandbox Code Playgroud)

借助此代码,我只能获得字体目录.我怎么能得到字体名称列表

"世纪教科书L","Ubuntu"等

提前致谢.

fal*_*tru 19

Try following:

>>> import matplotlib.font_manager
>>> [f.name for f in matplotlib.font_manager.fontManager.ttflist]
['cmb10', 'cmex10', 'STIXNonUnicode', 'STIXNonUnicode', 'STIXSizeThreeSym', 'STIXSizeTwoSym', 'cmtt10', 'STIXGeneral', 'STIXSizeThreeSym', 'STIXSizeFiveSym', 'STIXSizeOneSym', 'STIXGeneral', 'cmr10', 'STIXSizeTwoSym', ...]
>>> [f.name for f in matplotlib.font_manager.fontManager.afmlist]
['Helvetica', 'ITC Zapf Chancery', 'Palatino', 'Utopia', 'Helvetica', 'Helvetica', 'ITC Bookman', 'Courier', 'Helvetica', 'Times', 'Courier', 'Helvetica', 'Utopia', 'New Century Schoolbook', ...]
Run Code Online (Sandbox Code Playgroud)

  • @MartVandeVen,感谢您的评论.仅供参考,在Python 2.7中,也可以使用集合理解.`{f.name for f in matplotlib.font_manager.fontManager.afmlist}`.您也可以使用生成器表达式而不是列表推导(以避免创建中间列表对象). (4认同)
  • 它可能会渲染_a lot_的重复项,添加`set()`将为您提供无序排序的独特字体.所以,`set([f.name for f in matplotlib.font_manager.fontManager.afmlist])`和`set([f.name for f in matplotlib.font_manager.fontManager.ttflist])` (3认同)