matplotlib中文乱码问题解决
系统环境Ubuntu 18.04.4 LTS,利用Anconda配置Python虚拟环境,使用matplotlib画图,中文图注,字体SimHei。
下载字体与配置
https://www.fontpalace.com/font-download/SimHei/
利用
import matplotlib
print(matplotlib.matplotlib_fname())
out:
/home/ubuntu/anconda3/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc
则将字体文件SimHei.ttf放在
/home/ubuntu/anconda3/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf/
文件夹下。
随后,清除字体缓存,
利用
import matplotlib
print(matplotlib.get_cachedir())
out:
/root/.cache/matplotlib
清空该文件夹。
rm -rf /root/.cache/matplotlib
配置matplotlibrc
利用
import matplotlib
print(matplotlib.matplotlib_fname())
out:
/home/ubuntu/anconda3/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc
修改matplotlibrc文件,修改如下三处
保存。
配置font_manager.py
利用
import matplotlib
print(matplotlib.matplotlib_fname())
out:
/home/ubuntu/anconda3/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc
则,font_manager.py文件位置为:
/home/ubuntu/anconda3/lib/python3.7/site-packages/matplotlib/font_manager.py
修改如下两处:
PS:在vi中可利用 ‘\’ 定位需要修改的位置。