【问题标题】:How can i get list of font family(or Name of Font) in matplotlib如何在 matplotlib 中获取字体系列(或字体名称)列表
【发布时间】:2013-09-20 05:39:22
【问题描述】:

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

import matplotlib.font_manager
matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')

借助此代码,我只能获取字体目录。 我怎样才能得到像

这样的字体名称列表

“世纪教科书 L”、“Ubuntu”……等

提前致谢。

【问题讨论】:

    标签: python python-2.7 matplotlib


    【解决方案1】:

    尝试以下操作:

    >>> 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', ...]
    

    【讨论】:

    • 感谢好友快速回复:)
    • 它可能会呈现 很多 重复项,添加 set() 将为您提供冷漠排序的独特字体。所以,set([f.name for f in matplotlib.font_manager.fontManager.afmlist])set([f.name for f in matplotlib.font_manager.fontManager.ttflist])
    • @MartVandeVen,感谢您的评论。仅供参考,在 Python 2.7 中,集合理解也可用。 {f.name for f in matplotlib.font_manager.fontManager.afmlist}。您也可以使用生成器表达式而不是列表推导(以避免创建中间列表对象)。
    猜你喜欢
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    相关资源
    最近更新 更多