今天需要用matplotlib显示中文,下面及就分享一下我的个人解决过程,首先下载SimHei,下载网址为:
http://www.downcc.com/font/7665.html
然后打开一个py文件写入代码:
import matplotlib
print(matplotlib.matplotlib_fname())
会显示一个路径,我的路径为,把上面下载的SimHei放到该路径的fonts/ttf目录下,我的为:
/home/eric/anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf
上面代码显示的路径为:
/home/eric/anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc
font.serif : DejaVu Serif, Bitstream Vera Serif, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif,sans-serif
font.sans-serif : DejaVu Sans, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif,SimHei
axes.unicode_minus : False # use unicode for the minus symbol
# rather than hyphen. See
# http://en.wikipedia.org/wiki/Plus_and_minus_signs#Character_codes
修改这三项,然后在命令行里运行:
locate -b 'mpl-data'
我运行成功后,提示为:
➜ ~ locate -b 'mpl-data'
/home/eric/anaconda3/envs/py2/lib/python2.7/site-packages/matplotlib/mpl-data
/home/eric/anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data
/home/eric/anaconda3/pkgs/matplotlib-2.1.2-py36h0e671d2_0/lib/python3.6/site-packages/matplotlib/mpl-data
/home/eric/anaconda3/pkgs/matplotlib-2.2.3-py27hb69df0a_0/lib/python2.7/site-packages/matplotlib/mpl-data
/usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data
/usr/local/lib/python3.5/dist-packages/matplotlib/mpl-data
/usr/share/matplotlib/mpl-data
然后删除缓存:
cd ~
rm -r .cache/matplotlib
重新运行程序就会显示中文了啊。效果展示:
参考文献
[1]. ubuntu中matplotlib解决显示中文为方框. https://blog.youkuaiyun.com/weixin_30432007/article/details/95262018
[2]. Ubuntu16.04中matplotlib显示中文字体乱码解决方案. https://blog.youkuaiyun.com/zhanghm1995/article/details/89196300