gpt4 book ai didi

matplotlib 未检测到字体

转载 作者:行者123 更新时间:2023-12-01 22:55:20 25 4
gpt4 key购买 nike

当我将 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

最佳答案

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

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 Sans 的行为与 Homemade 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 italicoblique 例如),因此这也不是 Humor Sans 的错误。您的问题是文件格式不一致以及缺乏处理这些不一致的标准化代码的组合。

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

关于matplotlib 未检测到字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37407894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com