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文件起作用,然后在菜单中选择重启内核,清理输出
所有的输出都清理了,再次运行代码,不再报错!!!