1. 获取matplotlib所在的文件夹
import matplotlib
matplotlib.matplotlib_fname() #将会获得matplotlib包所在文件夹
2. 拷贝字体
将SimHei.ttf (可以在windows电脑上搜索找到)拷贝到 ~/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf
3. 修改配置文件
vi ~/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc
# 查找如下行,去掉注释,注册SimHei 字体
font.serif : SimHei, DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif
font.sans-serif : SimHei, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial,
axes.unicode_minus : False ## use unicode for the minus symbol 用来正常显示负号
4. 进入 ~/.cache/matplotlib 目录下,并删除matplotlib 下的fontList.josn
5. 重启python
6. 设置字体
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
sns.set(font='SimHei')
本文详细介绍了如何在Python环境中使用matplotlib显示中文,包括字体拷贝、配置文件修改、字体注册和重启Python等步骤。重点在于解决SimHei字体问题,确保负号显示和中文标签的正常显示。
1778

被折叠的 条评论
为什么被折叠?



