美文网首页Python
Mac python3 matplotlib中文乱码解决

Mac python3 matplotlib中文乱码解决

作者: fangqi179 | 来源:发表于2018-03-01 16:48 被阅读1545次

下载字体

http://www.fontpalace.com/font-download/SimHei/

复制字体到matplotlib字体目录下

cp SimHei.ttf ~/.pyenv/versions/3.6.4/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf

matplotlibrc配置文件修改

终端运行python后执行如下命令获取matplotlibrc配置文件路径

import matplotlib
matplotlib.matplotlib_fname()

记下地址,终端切换到该地址后修改matplotlibrc文件,添加如下内容:

vi matplotlibrc

添加内容:

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

其中SimHei为第一步下载的字体名称

清除matplotlib字体缓存

rm -rf ~/.matplotlib/*.cache
rm fontList.json

重新运行python程序即可正常显示中文

相关文章

网友评论

    本文标题:Mac python3 matplotlib中文乱码解决

    本文链接:https://www.haomeiwen.com/subject/rcfnxftx.html