错误RuntimeWarning: Glyph xxxxx missing from current font的产生原因解析,检测当前字体是否包含某字符
RuntimeWarning: Glyph xxxx missing from current font
是matplotlib
的经典错误。
原因大家都清楚:字体不匹配,显示不了对应的字符。
现象就是:本该显示的字符,显示为方框了,一般出现在汉字当中。
那这个错误是怎么产生的呢?
调试信息
d:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py:201:RuntimeWarning: Glyph 39064 missing from current font.font.set_text(s, 0, flags=flags)
根据traceback
信息可知:
- 提示当前字体中找不到
Glyph 39064
。 - 错误发生在
backend_agg.py
。 - 出错代码在
font.set_text(s, 0, flags=flags)
。
问题分析
Glyph
是象形文字的意思,可以理解为符号,那39064
是什么呢?通过查找汉字unicode编码表可知,39064
对应的汉字就是题
。从字面意思上看,错误信息就是表示在当前设置的字体中找不到对应字符
。不单汉字会出现这样的错误提示,只要当前字体中不存在对应字符都会报这个错误
。backend_agg.py
是matplotlib
的绘图后端模块。font.set_text(s, 0, flags=flags)
属于RendererAgg
类中的draw_text
方法。相关代码为:
font = self._get_agg_font(prop)if font is None:return None# We pass '0' for angle here, since it will be rotated (in raster# space) in the following call to draw_text_image).font.set_text(s, 0, flags=flags)
再次追踪 _get_agg_font(prop)。
def _get_agg_font(self, prop):"""Get the font for text instance t, caching for efficiency"""fname = findfont(prop)font = get_font(fname)font.clear()size = prop.get_size_in_points()font.set_size(size, self.dpi)return font
font = get_font(fname)
from matplotlib.font_manager import findfont, get_font
问题又回到了font_manager
模块了!
_get_font = lru_cache(64)(ft2font.FT2Font)
# FT2Font objects cannot be used across fork()s because they reference the same
# FT_Library object. While invalidating *all* existing FT2Fonts after a fork
# would be too complicated to be worth it, the main way FT2Fonts get reused is
# via the cache of _get_font, which we can empty upon forking (in Py3.7+).
if hasattr(os, "register_at_fork"):os.register_at_fork(after_in_child=_get_font.cache_clear)def get_font(filename, hinting_factor=None):# Resolving the path avoids embedding the font twice in pdf/ps output if a# single font is selected using two different relative paths.filename = os.path.realpath(filename)if hinting_factor is None:hinting_factor = rcParams['text.hinting_factor']return _get_font(os.fspath(filename), hinting_factor,_kerning_factor=rcParams['text.kerning_factor'])
from matplotlib import afm, cbook, ft2font, rcParams
ft2font
是matplotlib
的模块,在我的计算机上文件名是ft2font.cp37-win_amd64.pyd
,看来是用其他语言编译的模块。
通过查找可知,ft2font
模块的源码即https://github.com/matplotlib/matplotlib/blob/master/src/ft2font.cpp
。
static FT_UInt ft_get_char_index_or_warn(FT_Face face, FT_ULong charcode)
{FT_UInt glyph_index = FT_Get_Char_Index(face, charcode);if (!glyph_index) {PyErr_WarnFormat(NULL, 1, "Glyph %lu missing from current font.", charcode);// Apparently PyErr_WarnFormat returns 0 even if the exception propagates// due to running with -Werror, so check the error flag directly instead.if (PyErr_Occurred()) {throw py::exception();}}return glyph_index;
}
由此可知,这条错误从哪儿来了的!就是字体里没有对应的字符
!
检测当前字体是否含有该字符
根据fonttools
写了一个简易检测当前字体是否含有该字符的小工具。
import sys
from itertools import chainfrom fontTools.ttLib import TTFont
from fontTools.unicode import Unicodefont_path=r"c:\windows\fonts\simhei.ttf"
def font_validation(input_string,font_path):font =TTFont(font_path)chars = chain.from_iterable([y + (Unicode[y[0]],) for y in x.cmap.items()] for x in font["cmap"].tables)for i in input_string:char=ord(i) # 输出字符、10进制Unicode编号、16进制Unicode编号、Unicode名称、是否包含在字体内print(i,char,hex(char),Unicode[char],char in (x[0] for x in chars))font.close()font_validation("标题₩",font_path)
从结果看,韩元符号₩
黑体是没有的。
标 26631 0x6807 CJK UNIFIED IDEOGRAPH-6807 True
题 39064 0x9898 CJK UNIFIED IDEOGRAPH-9898 True
₩ 65510 0xffe6 FULLWIDTH WON SIGN False
使用黑体
绘图测试下:
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontPropertiesfont = FontProperties(fname=r"c:\windows\fonts\simhei.ttf", size=30)
plt.title("标题₩", fontproperties=font)
plt.show()
使用gulim
绘图测试下:
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontPropertiesfont_path=r"C:\Users\Administrator\AppData\Local\Microsoft\Windows\Fonts\gulim.ttc"
font = FontProperties(fname=font_path, size=30)
plt.title("标题₩", fontproperties=font)
plt.show()
错误RuntimeWarning: Glyph xxxxx missing from current font的产生原因解析,检测当前字体是否包含某字符相关推荐
- Matplotlib绘图显示缺少中文字体-RuntimeWarning: Glyph 8722 missing from current font.
pyplot 并不默认支持中文显示,也没有自带中文字体,因此需要自行下载所需字体,并修改 rcParams 参数来显示中文.下面以 SimHei (黑体)字体为例进行说明. 说明:系统环境为 Linu ...
- RuntimeWarning:Glyph 21435 missing from current font.
RuntimeWarning:Glyph 21435 missing from current font. 目录 RuntimeWarning:Glyph 21435 missing from cur ...
- RuntimeWarning: Glyph 30452 missing from current font. matplotlib画图无法显示中文
matplotlib画图无法显示中文 RuntimeWarning: Glyph 30452 missing from current font. plt画图的title显示的中文是方括号 错误图: ...
- python出现中文乱码 RuntimeWarning: Glyph 24180 missing from current font.解决方法
文章目录 遇到的问题 解决方法 参考 解决方法:在画图前添加这样一句代码 plt.rcParams['font.sans-serif'] = ['SimHei'] 遇到的问题 环境:win10,编辑器 ...
- python画图时,中文无法正常显示的问题,RuntimeWarning: Glyph 26631 missing from current font. font.set_text(s, 0, fl
python画图时,中文无法正常显示的问题,RuntimeWarning: Glyph 26631 missing from current font. font.set_text(s, 0, fla ...
- matplotlib解决中文乱码问题,或者RuntimeWarning: Glyph 20154 missing from current font.
matplotlib解决乱码问题 如图:为图表添加名称时报措,如何处理. 添加代码: from pylab import mplmpl.rcParams['font.sans-serif'] =[&q ...
- 出现RuntimeWarning: Glyph 39057 missing from current font.的解决办法:
出现RuntimeWarning: Glyph 39057 missing from current font.的解决办法: 在程序开头添加如下程序: import matplotlib as mpl ...
- RuntimeWarning: Glyph 24037 missing from current font. font.set_text(s, 0.0, flags=flags)解决方法
出现上图错误是因为plt画图时找不到字体,导致所画出的图出现下图中的情况: 会发现,横众坐标轴标题出现方框,无法正确显示字. 那么解决上述问题,只需要添加以下两行代码: plt.rcParams['f ...
- /backend_agg.py:238: RuntimeWarning: Glyph 26085 missing from current font.
linux 使用python matplotlib画图时没有字体显示中文,导致中文乱码 字体下载链接: https://github.com/Heath-Ledger/WorkFile/blob/ma ...
最新文章
- 二分图的最大匹配(匈牙利算法)HDU1083
- xtraback工具的使用和备份
- xcode 4.5 new feature __ ios6 新特性 (转)
- 静态html引入js添加随机数后缀防止缓存
- 祝贺本刊编委石勇教授入选2016年汤森路透全球高被引科学家
- 1.3编程基础之算术表达式与顺序执行 15 苹果和虫子
- Kafka简介与消息队列
- java经典源码 阅读_公开!阿里甩出“源码阅读指南”,原来源码才是最经典的学习范例...
- Java web实习报告
- IK10外壳冲击等级测试
- day048:LocalDateTime中增加、减少、直接修改时间的方法、计算时间间隔的方法
- Java实现HTML页面截图功能
- 免费ftp空间的文件传输
- 人工智能之父图灵之死:谜一样的解谜者
- 无线测温系统应用 对变电所进行实时的在线监测 生产高效安全
- 吃西瓜—先磨刀之概率论
- matlab确定分段函数的间断点,凡分段函数必有间断点
- Node.js的卸载
- 【C语言】案例四十六 点名册(二)【strcpy()函数】
- 修复lsp,360浏览器可以上网其它软件不行
热门文章
- 网页之家 v4.000 绿色
- 解决WinXP网络邻居共享问题
- 动态规划 背包问题小结 0-1背包(采药 九度第101题) 完全背包(Piggy-Bank POJ 1384) 多重背包(珍惜现在,感恩生活 九度第103题)
- 计算机专业答辩委员会评语,论文答辩小组评语参考
- 上微信怎么同时用计算机,如何同时登录两个或多个微信帐户?手机和计算机均可使用....
- freeswitch php监听,程序员罗杰-freeswitch对接asterisk压测
- 领课在线教育系统源码 各行业都适用的分布式在线教育系统+支持讲师入驻功能
- 3、Narcissistic Number - 打印出所有水仙花数
- .NETCore——异步
- CG14导弹防御系统