首先,Ubuntu下matplotlit没有微软雅黑字体,需要下载
1、第一步,首先下载字体https://github.com/StellarCN/scp_zh/blob/master/fonts/SimHei.ttf
2、将字体放在对应的ttf目录下,比如我的是:
/home/bing/anaconda3/envs/isce2/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf
3、修改matplotlibrc文件的配置,matplotlibrc文件再mpl-data目录下面
(1)249行:font.family: sans-serif #只是删除#号
(2)257行:font.sans-serif: SimHei, Bitstream Vera Sans, …#只是删除#号,然后在最前面加上SimHei
(3)400行:axes.unicode_minus: False#只是删除#号,然后把True改为False
4、删除~/.cache/matplotlib的缓冲目录
rm -rf ~/.cache/matplotlib
5、python中添加的操作,没有前面的步骤,下面代码是不会生效的
plt.rcParams['font.sans-serif']=['SimHei']#正常显示中文汉字 plt.rcParams['axes.unicode_minus']=False#解决保存图片为负号显示为方块的问题
6、重启生效
效果如下: