Font family [‘sans-serif‘] not found.Falling back to DejaVu Sans

刘超 1月前 ⋅ 331 阅读   编辑

一、描述

  mac使用matplotlib画图,有如下错误
 

二、分析

  1、执行如下命令,查看字体路径

(jddata_analysis_train) jiangxinyideAir:mpl-data jiangxinyi2$ python
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 03:13:28) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib    
>>> print(matplotlib.matplotlib_fname())
/Users/jiangxinyi2/venv/jddata_analysis_train/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc
>>> exit()
(jddata_analysis_train) jiangxinyideAir:mpl-data jiangxinyi2$ 

  2、打开“启动台”->“其他”->“字体册”,然后右击,选择“宋体-简”->“在访达中显示”,如下
 

  3、按Command+Option+c拷贝Songti.ttc的地址,然后cd到/Users/jiangxinyi2/venv/jddata_analysis_train/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf 目录,执行如下命令,拷贝Songti.ttc

cp /Library/Fonts/Songti.ttc  .

  4、编辑/Users/jiangxinyi2/venv/jddata_analysis_train/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc文件,修改如下内容

font.family         : sans-serif   
# 去掉前面的#     
font.sans-serif: Songti ,DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
# 去掉前面的#,并在冒号后面添加Songti
axes.unicode_minus  : False
# 去掉前面的#,并将True改为False

  5、删除matplotlib的缓存文件,如下

(jddata_analysis_train) jiangxinyideAir:mpl-data jiangxinyi2$ python
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 03:13:28) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.get_cachedir()
'/Users/jiangxinyi2/.matplotlib'
>>> 
>>> exit() 
(jddata_analysis_train) jiangxinyideAir:mpl-data jiangxinyi2$ rm -rf /Users/jiangxinyi2/.matplotlib
(jddata_analysis_train) jiangxinyideAir:mpl-data jiangxinyi2$ 

  6、重启pycharm,还是报Font family [‘sans-serif‘] not found.Falling back to DejaVu Sans错误

  7、难道只支持ttf格式的,不支持ttc的,再拷贝/Library/Fonts/Arial Bold.ttf到/Users/jiangxinyi2/venv/jddata_analysis_train/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf目录,这里改一下名字,避免因为空格原因导致报其他错误 mv Arial\ Bold.ttf Arial-Bold.ttf 

  8、编辑/Users/jiangxinyi2/venv/jddata_analysis_train/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc文件,修改如下内容

font.sans-serif:  Arial-Bold, Songti,DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif

  9、删除matplotlib的缓存文件,重启pycharm,还不是不行

  10、从http://www.xiazaiziti.com/210356.html下载SimHei.ttf字体,然后放到/Users/jiangxinyi2/venv/jddata_analysis_train/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf目录下,再修改/Users/jiangxinyi2/venv/jddata_analysis_train/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc文件

font.sans-serif:  SimHei, Songti,DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif

  再删除matplotlib的缓存文件,重启pycharm

三、解决方法

  从http://www.xiazaiziti.com/210356.html下载SimHei.ttf字体,然后放到/Users/jiangxinyi2/venv/jddata_analysis_train/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf目录下,按如下信息修改/Users/jiangxinyi2/venv/jddata_analysis_train/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc文件

font.family         : sans-serif   
# 去掉前面的#     
font.sans-serif:  SimHei, Songti,DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
# 去掉前面的#,并在冒号后面添加Songti
axes.unicode_minus  : False
# 去掉前面的#,并将True改为False

  再删除matplotlib的缓存文件,重启pycharm

四、参考文章
  1、https://blog.csdn.net/sinat_40875078/article/details/104326855
  2、http://www.xiazaiziti.com/210356.html


注意:本文归作者所有,未经作者允许,不得转载

全部评论: 0

    我有话说: