Linux中matplotlib 中文显示问题解决


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

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

  • 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

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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM