Matplotlib无法检测到字体。

3

当我使用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

运行 fc-list | grep -i humor 命令是否有输出?如果没有,尝试运行 fc-cache -s 命令(重新构建字体缓存)后再次检查。 - grochmal
4
我用 rm .cache/matplotlib/fontList.py3k.cache 解决了这个问题。对于某些人来说,它可能在 ~/.matplotlib/fontList.cache 中。 - lincolnfrias
现在我有一个相关问题:音调没有正确显示:"DISTÀNCIA" 显示为 "DIST?NCIA"。这个字体有任何 UTF 问题吗? - somenxavier
我有同样的问题。我没有找到任何解决方法。 - lincolnfrias
1
@lincolnfrias 我问了关于这个的问题。 - somenxavier
显示剩余2条评论
2个回答

3

好的,经过仔细审视后,发现这是一个错误。使用以下测试:

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软件包的免费谷歌字体)的行为。问题在于matplotlib会对fontname=中指定的字体名称进行匹配,该匹配不仅使用名称,还使用字体的多个属性。您可以在/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 italicoblique),因此这也不是Humor Sans的问题。你面临的问题是文件格式的不一致性和缺乏统一的代码来处理它们的组合。
我建议找一个外观类似的不同字体。编辑TTF或matplotlib代码都很棘手,可能会导致其他问题。

那么,我们应该针对 humor sans 包还是 matplotlib 包提交一个 bug?感谢您的所有更正和代码。 - somenxavier
我认为双方都有责任:matplotlib 假设字体的太多,而 Humor Sans 则没有尽可能地兼容。然而,我相信这种错误不会得到修复。在 matplotlib 中编辑匹配代码太容易出错了,而字体包(Humor Sans)通常是未维护的(例如,我打包了 Homemade Apple,但我没有拥有字体文件,如果该字体出现此类错误,我将无能为力)。 - grochmal
那么我们要怎么解决这个问题呢? - somenxavier

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接