plt绘图提示:findfont: Generic family ‘sans-serif‘ not found because none of the following families were

plt绘图提示:findfont: Generic family ‘sans-serif’ not found because none of the following families were found: SimHei

1.问题描述

在jupyter中用plt绘图,代码中设置:

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

在jupyter中绘图时,plt绘图提示:

findfont: Generic family ‘sans-serif’ not found because none of the following families were found: SimHei

2.处理过程

(1)增加字符集

由于使用anaconda3 ,在虚拟环境中的字符集路径下,增加SimHei.ttf字符文件。

/root/anaconda3/envs/py38/lib/python3.8/site-packages/matplotlib/mpl-data/fonts/ttf/SimHei.ttf

执行代码,还是报错。

(2)检查缓存字符集
import matplotlib as mpl
print(mpl.get_cachedir())

路径:/root/.cache/matplotlib

缓存字符集文件,fontlist-v330.json ,查询文件中没有显示 SimHei.ttf

(3)清除运行缓存

删除 字符集缓存文件 : fontlist-v330.json

安装 fontconfig

yum install fontconfig -y

刷新字体缓存

# fc-cache -fv

执行程序,报错依旧

(4)检查缓存

fontlist-v330.json ,已经有字体的配置信息了

    {
      "fname": "fonts/ttf/SimHei.ttf",
      "name": "SimHei",
      "style": "normal",
      "variant": "normal",
      "weight": 400,
      "stretch": "normal",
      "size": "scalable",
      "__class__": "FontEntry"
    },
 

但是在jupyter中,还是报错。

(5)jupyter重启内核

选中执行报错的程序文件,重启内核是对每一个notebook文件起作用,然后在菜单中选择重启内核,清理输出
在这里插入图片描述
所有的输出都清理了,再次运行代码,不再报错!!!

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值