1. 问题
在 Linux Ubuntu/Centos 系统下,Python 画图/保存图片出现报错:
not found because none of the following families were found: Times New Roman"
原因是缺少 “Times New Roman” 字体,需要手动下载
2. 解决步骤
2.1 下载 Times New Roman
# ubuntu
sudo apt-get update
sudo apt-get install ttf-mscorefonts-installer
# centos
yum install ttf-mscorefonts-installer
如果是非图形化的系统,会出现一个界面,移动上下左右键,选择 “OK”安装。
2.2 删除字体缓存
命令行输入:Python
进入到 Python 终端界面
import matplotlib as mpl
print(mpl.get_cachedir())
删除缓存:
rm -rf /root/.cache/matplotlib/*
2.3 下载字体
进入到 ~/.fonts
目录下(如果没有目录,那就创建 mkdir ~/.fonts
)
cd ~/.fonts
在这里链接下载 SimHei 字体,或者执行下面命令:
wget http://129.204.205.246/downloads/SimHei.ttf
2.4 下载fc-cache命令
# centos
sudo yum install fontconfig -y
# ubuntu
sudo apt-get install fontconfig -y
# mac
brew install fontconfig
刷新字体缓存
fc-cache -fv
3. 使用字体
import matplotlib.pyplot as plt
plt.rc('font', family="Times New Roman")