matplotlib不检测字体

matplotlib不检测字体,matplotlib,archlinux,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.def

当我将
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]))
我已经安装了
幽默Sans
。我使用archlinux并安装了这个包

我已确保字体配置缓存
fc列表
找到
字体:

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

嗯,仔细看后,它是一种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')
我比较了
幽默Sans
自制苹果(一种免费的谷歌字体,我把它打包成了一个AUR软件包)的行为。问题是
matplotlib
fontname=
中指定的字体名称进行了匹配,匹配不仅使用了名称,还使用了字体的多个属性。在
/home/grochmal/mat3/lib/python3.5/site packages/matplotlib/font\u 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
不需要
幽默Sans
中缺少的字段,公平地说,TTF中字体的描述存在一些不一致之处(例如谷歌斜体与斜体),因此也不是
幽默Sans
的错误。您的问题是文件格式中的不一致性与缺乏处理这些不一致性的标准化代码相结合


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

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


参见

fc list | grep-i humor
是否提供任何输出?如果您运行
fc cache-s
(重建字体缓存)并再次检查,它会给我:
$fc list | grep-i
humor/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你是赖特。这是solution@lincolnfrias我问过,我们应该针对humor sans软件包还是matplotlib软件包提交一个bug?谢谢你所有的更正和代码。我认为两者都是。在某种程度上,两者都有错:
matplotlib
对字体的假设太多,而
幽默Sans
则没有尽可能地兼容。然而,我相信,这是一种无法修复的错误。在
matplotlib
中编辑匹配的代码太容易出错,字体包(
Humor Sans
)经常无法维护(例如,我的软件包
自制的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