在 Ubuntu 上安装Times New Roman 字体
📌 背景说明
在使用 Python + matplotlib 绘图时,可能出现如下提示:
findfont: Font family 'Times New Roman' not found.
原因是 Ubuntu 默认未安装 Times New Roman。matplotlib 依赖系统字体库,如果系统中没有该字体,就会回退到其它字体,导致论文/报告的排版与预期不符。
✅ 解决过程
1) 安装微软核心字体
Ubuntu 官方源提供了 ttf-mscorefonts-installer 包,可以一次性安装常用的微软字体(Times New Roman、Arial、Verdana 等)。
sudo apt update
sudo apt install ttf-mscorefonts-installer
安装过程中会提示你阅读并接受 EULA(许可协议),使用 Tab → Enter 即可确认。
2) (可选)安装开源字体
为了获得更多可用字体,可以一并安装:(这些是开源字体集合,兼容性也不错。)
sudo apt install fonts-dejavu-core fonts-freefont-ttf
3) 刷新字体缓存 & 清理 matplotlib 缓存
matplotlib 会缓存系统字体列表。如果你之前运行过 matplotlib,需要清理缓存,确保新字体能被识别:
sudo fc-cache -f -v
rm -rf ~/.cache/matplotlib
4) 在 Python 中验证Times New Roman 是否可用
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'Times New Roman'
print(plt.rcParams['font.family'])
如果输出结果包含 'Times New Roman',说明字体已经安装成功。
5) (额外建议)设为全局默认
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = 'Times New Roman'
这样就不用在每次画图时手动指定字体了。
✅ 小结
解决核心:安装 ttf-mscorefonts-installer,刷新字体缓存,清理 matplotlib 缓存并验证。
GPT 也给了一个脚本,可以在这里下载:install_times_new_roman_ubuntu.sh