Times New Roman是许多期刊和机构的标准字体,而Times New Roman是微软的专有字体,Ubuntu官方仓库可能不提供,所以需要手动安装。此外python绘图也需要调用该字体,因此正确安装对于用户来说很重要。
安装思路:
在 Windows 系统通过 SSH 将本地字体文件复制到 Linux 服务器
安装步骤:
1. 准备 Windows 环境(windows端操作)
(1) 确认本地字体路径
Windows 系统自带times new roman字体,本地字体文件的标准路径为:
C:\Windows\Fonts\times.ttf # 常规
C:\Windows\Fonts\timesbd.ttf # 粗常规
C:\Windows\Fonts\timesi.ttf # 斜体
C:\Windows\Fonts\timebi.ttf # 粗斜体
2. 通过 SCP 传输文件(windows端操作)
(1) 打开 PowerShell
以管理员身份运行 PowerShell。
(2) 执行 SCP 命令
powershell输入
# 传输单个文件
scp "C:\Windows\Fonts\timesi.ttf" username@server_ip:~/timesi.ttf
# 批量传输所有 Times 字体
scp "C:\Windows\Fonts\times*.ttf" username@server_ip:~/
注意:
-
用实际值替换
username
(服务器用户名)和server_ip
(服务器IP,服务器终段输入ip a查看) -
路径含空格或特殊字符时需用双引号包裹
3. 服务器端安装(windows端操作)
注:服务器终端直接操作同理
(1) PowerShell通过SSH 登录ubuntu服务器,替换实际的用户名和ip地址
ssh username@server_ip
(2) 移动字体到系统目录
# 创建字体目录(如不存在)
sudo mkdir -p /usr/share/fonts/truetype/custom
# 移动字体文件
sudo mv ~/times*.ttf /usr/share/fonts/truetype/custom/
# 修改权限
sudo chmod 644 /usr/share/fonts/truetype/custom/times*.ttf
(3) 刷新字体缓存
sudo fc-cache -fv
4. 验证安装
fc-list | grep -i "Times New Roman"
正常输出应包含:
/usr/share/fonts/truetype/custom/timesi.ttf: Times New Roman:style=Italic
小结:
1. 在Windows上打开命令提示符或PowerShell。
2. 使用SCP命令将本地的字体文件上传到服务器的用户主目录。
3. 通过SSH登录服务器,使用sudo将文件移动到系统字体目录。
4. 设置文件权限并更新字体缓存。
5.python调用
实现python使用times new roman字体绘图,并且数学文本也能使用与 Times New Roman 兼容的字体显示
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import font_manager
# 注册字体文件
# 指定 Times New Roman 字体文件路径
font_files = [
"/usr/share/fonts/truetype/custom/times.ttf", # Times New Roman 正常体
"/usr/share/fonts/truetype/custom/timesbd.ttf", # Times New Roman 粗体
"/usr/share/fonts/truetype/custom/timesi.ttf", # Times New Roman 斜体
"/usr/share/fonts/truetype/custom/timesbi.ttf" # Times New Roman 粗斜体
]
# 将字体文件添加到 matplotlib 的字体管理器中
for file in font_files:
font_manager.fontManager.addfont(file)
# 配置全局字体设置
plt.rcParams.update({
"font.family": "Times New Roman", # 设置全局字体为 Times New Roman
"axes.labelsize": 24, # 设置轴标签字体大小
"xtick.labelsize": 20, # 设置 x 轴刻度标签字体大小
"ytick.labelsize": 20, # 设置 y 轴刻度标签字体大小
"mathtext.fontset": "stix" # 确保数学文本使用与 Times New Roman 兼容的字体
})
# 设置刻度方向为向内
plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['ytick.direction'] = 'in'
# 绘制一个简单的正弦波图来验证字体设置
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.figure(figsize=(10, 6))
plt.plot(x, y, label=r'$y = \sin(x)$') # 使用 LaTeX 格式标签
# 设置标题和标签
plt.title('Sine Wave Example', fontsize=24)
plt.xlabel(r'$x$', fontsize=24) # 使用 LaTeX 格式
plt.ylabel(r'$y = \sin(x)$', fontsize=24)
# 设置图例
plt.legend(fontsize=20)
# 显示网格
plt.grid(True)
# 显示图形
plt.show()