【问题标题】:matplotlib does not detect fontmatplotlib 不检测字体
【发布时间】:2020-12-07 17:42:51
【问题描述】:

当我将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(重建字体缓存)并再次检查?
  • 它给了我:$ fc-list | grep -ihumor/usr/share/fonts/TTF/Humor-Sans-1.0.ttf: Humor Sans:style=Regular。在fc-cache -s 之后问题仍然存在
  • 我用 rm .cache/matplotlib/fontList.py3k.cache 解决了这个问题。对于某些人来说,它会在 ~/.matplotlib/fontList.cache
  • @lincolnfrias 你是莱特。这是解决方案
  • @lincolnfrias 我要求that

标签: matplotlib archlinux


【解决方案1】:

好吧,仔细看后,它是一种错误。使用以下测试:

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 包中的免费 google 字体)的行为。问题是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 vs. oblique示例)因此也不是Humor Sans 错误。您的问题是文件格式不一致以及缺乏处理它们的标准化代码的组合。

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

【讨论】:

  • 那么,我们应该针对幽默 sans 包还是针对 matplotlib 包提交错误?感谢您的所有更正和代码。
  • 我认为两者都是。两者在某种程度上都有问题:matplotlib 对字体的假设过多,Humor Sans 没有尽可能地兼容。然而,我相信,这是一种无法修复的错误。在matplotlib 中编辑匹配代码太容易出错,并且字体包(Humor Sans)通常无人维护(例如,我包Homemade Apple 但不拥有字体文件,如果该字体出现这样的错误,我将无能为力)。
  • 那么我们将如何解决这个问题?
【解决方案2】:

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

https://stackoverflow.com/a/22812176

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-06
    • 2017-07-04
    • 2017-06-25
    • 1970-01-01
    • 2011-10-16
    • 2011-02-01
    • 2010-10-11
    相关资源
    最近更新 更多