One - One Code All

Blog Content

Mac python3 matplotlib中文乱码解决

Linux-Mac   2020-03-22 11:46:45

Mac python3 matplotlib中文乱码解决

 

下载字体

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


复制字体到matplotlib字体目录下

cp SimHei.ttf  /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf


matplotlibrc配置文件修改

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

import matplotlib
matplotlib.matplotlib_fname()


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


vi matplotlibrc

添加内容,将已有内容前面的#去掉,并将SimHei添加进font.sans-serif:

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 ~/.matplotlib/fontList.json


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



上一篇:mac 查看字体
下一篇:如何查看vue版本号以及vue-cli版本号

The minute you think of giving up, think of the reason why you held on so long.