第1步:下载Times New Roman字体
链接:https://pan.baidu.com/s/1kORdQHI15HNz9A57SPvd7Q?pwd=ad80
提取码:ad80
第2步:将上述下载的4个ttf文件放在如下目录:
~/lib/python3.8/site-packages/matplotlib/mpl-data/fonts/ttf
其中:~/为python环境目录,如果是anaconda创建的环境,一般为anaconda3/envs/python环境名称/
,另外上述“python3.8”是你的环境的python版本号。
第3步:清楚缓存(必须)。
查看缓存的地址:
import matplotlib
print(matplotlib.get_cachedir()) #假设是/home/xxx/.cache/matplotlib
命令行cd到上述目录/home/xxx/
下,执行:rm -rf .cache/matplotlib
第4步:重启notebook
第5步:matplotlib设置字体代码
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Times New Roman']