1.下载下载中文 arial unicode ms 字体到 /home 目录

2. 拷贝字体到 usr/share/fonts 下:

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
  • sudo cp ~/arial\ unicode\ ms.ttf /usr/share/fonts/arial\ unicode\ ms.ttf

3. 修改配置文件,运行下列代码查看配置文件位置

  • import matplotlib print(matplotlib.matplotlib_fname()) #输出配置文件位置 # eg:/home/python/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc

进入到上述找到的配置文件中,修改下面两项

sudo vim /home/python/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc
# 打开这一行注释
font.family         : sans-serif # 在原有字体之前,新增我们刚下载的中文字体 arial unicode ms
font.sans-serif     : arial unicode ms, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif

 4. 这一步骤最重要!为matplotlib增加中文字体.将下载的arial unicode ms.ttf字体文件复制一份到步骤2找到的配置文件的同级文件夹fonts中:

sudo cp ~/arial\ unicode\ ms.ttf /home/python/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/arial\ unicode\ ms.ttf

 5. 清除本地matplotlib中的缓存

  • rm -rf ~/.cache/matplotlib

现在就可以重新画图,查看中文效果了。

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄