Python matplotlib 乐成利用SimHei 中文字体

打印 上一主题 下一主题

主题 1818|帖子 1818|积分 5454

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企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

大连密封材料

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表