原文出处:https://blog.csdn.net/sinat_40875078/article/details/104326855
1、在终端python 的环境下查看字体路径
import matplotlib
print(matplotlib.matplotlib_fname())
2.下载SimSun字体(宋体)
以下我给出了GitHub下载链接
链接:GitHub
- SimSun.ttf 是宋体(学术论文常用)
- SimHei.ttf 是黑体
并前往指定目录:/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf
,将下载好的SimHei或SimSun移动到该目录下。
3.修改matplotlibrc文件
修改/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc
,修改内容如下:
font.family : sans-serif
# 去掉前面的#
font.sans-serif : SimSun, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
# 去掉前面的#,并在冒号后面添加SimHei
axes.unicode_minus : False
# 去掉前面的#,并将True改为False
千万记得拼对,笔者第一次就错写成了SimSum
,emmmm
4.清除matplotlibrc缓存
在终端python 的环境下,输入如下指令,查看matplotlib的字体缓存路径:
import matplotlib
matplotlib.get_cachedir()
然后使用rm -rf +路径
删除缓存
然后重启软件即可,如果是Centos得reboot一下才能生效
结语
最后在用matplotlib绘图的时候别放了添加
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimSun']
plt.rcParams['axes.unicode_minus'] = False