1. 执行命令,获取matplotlibrc文件路径
import matplotlib
print(matplotlib.matplotlib_fname())
如下
/Users/haiping.lai/miniconda3/lib/python3.9/site-packages/matplotlib/mpl-data/matplotlibrc
2. 修改matplotlibrc
font.family: sans-serif
font.sans-serif: SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
axes.unicode_minus: False
3. 下载SimHei.ttf文件,地址:SimHei.ttf|字体下载,将文件放到指定路径
/Users/haiping.lai/miniconda3/lib/python3.9/site-packages/matplotlib/mpl-data/fonts/ttf/SimHei.ttf
4. 重新加载matplotlib
- 方法一
from matplotlib.font_manager import _rebuild
_rebuild() #reload一下
可能会出现如下错误,则采用方法二
ImportError: cannot import name '_rebuild' from 'matplotlib.font_manager' (/Users/haiping.lai/miniconda3/lib/python3.9/site-packages/matplotlib/font_manager.py)
- 方法二
import shutil
import matplotlibshutil.rmtree(matplotlib.get_cachedir())