qidao123.com技术社区-IT企服评测·应用市场

标题: Python matplotlib 乐成利用SimHei 中文字体 [打印本页]

作者: 大连密封材料    时间: 2025-5-7 09:52
标题: Python matplotlib 乐成利用SimHei 中文字体
1. 背景

Python 利用 matplotlib 库 设置中文字体时
  1. plt.rcParams["font.sans-serif"] = ["SimHei"]
  2. plt.rcParams["axes.unicode_minus"] = False
复制代码
出现下面的错误:
  1. UserWarning: Glyph 25968 (\N{CJK UNIFIED IDEOGRAPH-6570}) missing from font(s) DejaVu Sans.
  2.   plt.tight_layout()
  3. findfont: Generic family 'sans-serif' not found because none of the following families were found: SimHei
  4. findfont: Generic family 'sans-serif' not found because none of the following families were found: SimHei
  5. findfont: Generic family 'sans-serif' not found because none of the following families were found: SimHei
  6. findfont: Generic family 'sans-serif' not found because none of the following families were found: SimHei
  7. findfont: Generic family 'sans-serif' not found because none of the following families were found: SimHei
  8. findfont: Generic family 'sans-serif' not found because none of the following families were found: SimHei
复制代码
报错信息表明在绘制图形时,系统缺少所需的字体(如SimHei),导致无法正确显示中文字符
2. 下载

下载地址:
https://gitcode.com/open-source-toolkit/c55ef/?utm_source=tools_gitcode&index=top&type=card
下载后更名
  1. sudo mv Ubuntu_18.04_SimHei.ttf SimHei.ttf
复制代码
3. 更新

3.1 将下载的SimHei.ttf文件移动到系统的字体目次中

  1. sudo mv SimHei.ttf /usr/share/fonts/truetype/
复制代码
3.2 更新系统的字体缓存

  1. sudo fc-cache -fv
复制代码
3.3 查看系统中的字体

  1. $ fc-list | grep "SimHei"
  2. /usr/share/fonts/truetype/SimHei.ttf: SimHei,黑体:style=Regular
复制代码
3.4 清算 matplotlib 缓存目次

  1. rm ~/.cache/matplotlib -fr
复制代码
3.5 重新启动IDE运行代码

天生图片如下

4. 参考

Matplotlib 找不到基本字体

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 qidao123.com技术社区-IT企服评测·应用市场 (https://dis.qidao123.com/) Powered by Discuz! X3.4