matplotlib 不检测字体 [英] matplotlib does not detect font
问题描述
当我将 fontname=
与 Humor Sans
字体一起使用时,出现此错误:
/usr/lib/python3.5/site-packages/matplotlib/font_manager.py:1288: UserWarning: findfont: 字体系列 ['Humor Sans', 'Comic Sans MS'] 未找到.回到 Bitstream Vera Sans(prop.get_family(), self.defaultFamily[fonttext]))
我安装了 Humor Sans
.我使用 archlinux 并安装了 ttf-humor-sans 包.>
我已经确保字体配置缓存 fc-list
找到了 Humor Sans
字体:
$ fc-list |grep -i 幽默/usr/share/fonts/TTF/Humor-Sans-1.0.ttf: Humor Sans:style=Regular
好吧,仔细一看,算是个bug吧.使用以下测试:
将 matplotlib.font_manager 导入为 fm导入 matplotlib.pyplot 作为 pltfont_cache = [i for i infm.findSystemFonts(fontpaths=None, fontext='ttf')如果'umor'在我]对于我在 font_cache 中:打印(一)fig = plt.figure()ax = fig.add_subplot(111)ax.plot([1],[1],'o')ax.set_title('我的标题', fontname='Humor Sans')#ax.set_title('我的标题', fontname='自制苹果')fig.savefig('tmp.png')
我将 Humor Sans
与 Homemade Apple
(我打包到 AUR 包中的免费谷歌字体)的行为进行了比较.问题是 matplotlib
对 fontname=
中指定的字体名称进行匹配,匹配不仅使用名称,还使用以下几个属性字体.在 /home/grochmal/mat3/lib/python3.5/site-packages/matplotlib/font_manager.py
你看到匹配:
用于字体列表中的字体:如果(目录不是 None 并且os.path.commonprefix([font.fname, directory]) != directory):继续# 匹配的家庭应该是最高优先级,所以是相乘的# 10.0分数 = \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)如果分数<最好成绩:best_score = 分数best_font = 字体如果分数 == 0:休息
不幸的是 Humor Sans
永远不会到达匹配阶段,因为不是所有的 prop.get_...
都可以填写.本质上它永远不会被包含在 fontlist 中代码>.
Homemade Apple
包含在内,因为它可以填充所有属性.
字体属性的不同可以看出如下:
[me@haps aur]# otfinfo --info/usr/share/fonts/TTF/HomemadeApple.ttf家庭:自制苹果亚科:常规全名:自制苹果后记名称:自制苹果首选家庭:自制苹果首选亚科:常规Mac字体菜单名称:自制苹果版本:1.000 版唯一 ID:FontDiner,Inc:自制苹果:2010说明: Font Diner, Inc. 版权所有 (c) 2010.保留所有权利.设计师:Font Diner, Inc设计师网址:http://www.fontdiner.com制造商:Font Diner, Inc供应商网址:http://www.fontdiner.com商标:Homemade Apple 是 Font Diner, Inc. 的商标.版权所有:Font Diner, Inc. 版权所有 (c) 2010.保留所有权利.许可证网址:http://www.apache.org/licenses/LICENSE-2.0许可证说明:在 Apache 许可证下获得许可,版本 2.0供应商 ID:DINR[me@haps aur]# otfinfo --info/usr/share/fonts/TTF/Humor-Sans-1.0.ttf家庭: Humor Sans亚科:常规全名: Humor Sans后记名称:HumorSans版本:2.9 版 28/3/09唯一 ID:Fontifier 2.9 (172) www.fontifier.com Humor Sans版权所有:版权所有 (c) Randall Munroe 最大的粉丝 2009.由 www.fontifier.com 创建.usa-1lip-4fvu15供应商 ID:Alts
Humor Sans
中缺少的字段不是必需的,公平地说,TTF 中字体的描述方式存在一些不一致(google italic 与 oblique 例如)因此它也不是 Humor Sans
的错.您的问题是文件格式不一致以及缺乏处理它们的标准化代码的组合.
我建议找到一种看起来足够相似的不同字体.编辑 TTF 或 matplotlib 代码非常棘手,可能会导致其他问题.
When i use fontname=
with the Humor Sans
font I get this error:
/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]))
I have the Humor Sans
installed. I use archlinux and I installed the ttf-humor-sans package.
I have ensured that the font config cache fc-list
finds the Humor Sans
font:
$ fc-list | grep -i Humor
/usr/share/fonts/TTF/Humor-Sans-1.0.ttf: Humor Sans:style=Regular
Well, after properly looking at it, it is a kind of a bug. Using the following test:
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')
I have compared the behaviour of the Humor Sans
against Homemade Apple
(a free google font that i package into an AUR package). And the issue is that matplotlib
does matching on the font names specified in fontname=
, the matching does not use only the name but several properties of the font. in /home/grochmal/mat3/lib/python3.5/site-packages/matplotlib/font_manager.py
you see the matching:
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
Unfortunately Humor Sans
never reaches the matching phase because not all prop.get_...
can be filled in. In essence it never gets included into fontlist
. Homemade Apple
is included because it can fill all the properties.
The difference in font properties can be seen as follows:
[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
There missing fields from Humor Sans
are not required, to be fair there are several inconsistencies in how fonts are described inside TTF (google italic vs. oblique for an example) therefore it isn't Humor Sans
fault either. Your issue is a combination of inconsistencies in a file format with a lack of standardised code to deal with them.
I would suggest to find a different font that looks similar enough. Either editing the TTF or the matplotlib code is very tricky and might result in other problems.
这篇关于matplotlib 不检测字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!