matplotlib不检测字体

时间:2016-05-24 08:14:24

标签: matplotlib archlinux

当我使用fontname=字体Humor Sans时,我收到此错误:

/usr/lib/python3.5/site-packages/matplotlib/font_manager.py:1288: UserWarning: findfont: Font family ['Humor Sans', 'Comic Sans MS'] not found. Falling back to Bitstream Vera Sans
  (prop.get_family(), self.defaultFamily[fontext]))

我安装了Humor Sans。我使用archlinux并安装了ttf-humor-sans包。

我确保字体配置缓存fc-list找到Humor Sans字体:

$ fc-list | grep -i Humor
/usr/share/fonts/TTF/Humor-Sans-1.0.ttf: Humor Sans:style=Regular

2 个答案:

答案 0 :(得分:2)

好吧,经过适当的查看,这是一种bug。使用以下测试:

import matplotlib.font_manager as fm
import matplotlib.pyplot as plt

font_cache = [i for i in
    fm.findSystemFonts(fontpaths=None, fontext='ttf')
    if 'umor' in i]
for i in font_cache:
    print(i)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1],[1],'o')
ax.set_title('My Title', fontname='Humor Sans')
#ax.set_title('My Title', fontname='Homemade Apple')
fig.savefig('tmp.png')

我已将Humor SansHomemade Apple(我打包的免费谷歌字体到AUR包中)的行为进行了比较。问题是matplotlibfontname=中指定的字体名称匹配,匹配不仅使用名称,而且使用字体的几个属性。在/home/grochmal/mat3/lib/python3.5/site-packages/matplotlib/font_manager.py中,您会看到匹配:

for font in fontlist:
    if (directory is not None and
        os.path.commonprefix([font.fname, directory]) != directory):
        continue
    # Matching family should have highest priority, so it is multiplied
    # by 10.0
    score = \
        self.score_family(prop.get_family(), font.name) * 10.0 + \
        self.score_style(prop.get_style(), font.style) + \
        self.score_variant(prop.get_variant(), font.variant) + \
        self.score_weight(prop.get_weight(), font.weight) + \
        self.score_stretch(prop.get_stretch(), font.stretch) + \
        self.score_size(prop.get_size(), font.size)
    if score < best_score:
         best_score = score
         best_font = font
    if score == 0:
         break

不幸的是Humor Sans永远不会到达匹配阶段,因为并非所有prop.get_...都可以填写。实质上它永远不会被包含在fontlist中。包含Homemade Apple是因为它可以填充所有属性。

字体属性的差异可以看作如下:

[me@haps aur]# otfinfo --info /usr/share/fonts/TTF/HomemadeApple.ttf
Family:              Homemade Apple
Subfamily:           Regular
Full name:           Homemade Apple
PostScript name:     HomemadeApple
Preferred family:    Homemade Apple
Preferred subfamily: Regular
Mac font menu name:  Homemade Apple
Version:             Version 1.000
Unique ID:           FontDiner,Inc: Homemade Apple: 2010
Description:         Copyright (c) 2010 by Font Diner, Inc. All rights reserved.
Designer:            Font Diner, Inc
Designer URL:        http://www.fontdiner.com
Manufacturer:        Font Diner, Inc
Vendor URL:          http://www.fontdiner.com
Trademark:           Homemade Apple is a trademark of Font Diner, Inc.
Copyright:           Copyright (c) 2010 by Font Diner, Inc. All rights reserved.
License URL:         http://www.apache.org/licenses/LICENSE-2.0
License Description: Licensed under the Apache License, Version 2.0
Vendor ID:           DINR

[me@haps aur]# otfinfo --info /usr/share/fonts/TTF/Humor-Sans-1.0.ttf
Family:              Humor Sans
Subfamily:           Regular
Full name:           Humor Sans
PostScript name:     HumorSans
Version:             Version 2.9 28/3/09
Unique ID:           Fontifier 2.9 (172) www.fontifier.com Humor Sans
Copyright:           Copyright (c) Randall Munroe's biggest fan 2009. Created by www.fontifier.com. usa-1lip-4fvu15
Vendor ID:           Alts

Humor Sans中缺少字段不是必需的,公平地说,TTF中的字体描述方式有几处不一致(google italic 倾斜例如,因此它也不是Humor Sans错误。您的问题是文件格式不一致,缺乏标准化代码来处理它们。

我建议找一个看起来很相似的不同字体。编辑TTF或matplotlib代码非常棘手,可能会导致其他问题。

答案 1 :(得分:1)

AUR的Humor Sans为我工作。但是,我必须先删除字体缓存:
rm ~/.cache/matplotlib/fontlist-v330.json

请参见https://stackoverflow.com/a/22812176