You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python3.9.13下Matplotlib无法实现Times New Roman斜体Å符号标注

解决Matplotlib中Times New Roman斜体Å符号的显示问题

问题分析

需要将x轴标签设置为Times New Roman字体的“Displacement”,同时搭配Times New Roman斜体的Å符号,但现有代码要么Å是斜体但非Times New Roman,要么是Times New Roman但无法斜体。

解决方案

通过配置Matplotlib的mathtext参数,指定数学模式下的斜体字体为Times New Roman,同时全局设置字体为Times New Roman,即可实现需求:

import matplotlib.pyplot as plt

# 配置mathtext使用自定义字体,指定罗马体和斜体为Times New Roman
plt.rcParams['mathtext.fontset'] = 'custom'
plt.rcParams['mathtext.rm'] = 'Times New Roman'
plt.rcParams['mathtext.it'] = 'Times New Roman:italic'

# 设置全局字体为Times New Roman,确保"Displacement"使用该字体
plt.rcParams['font.family'] = 'Times New Roman'

# 设置x轴标签,数学模式下的\textit{\AA}会调用配置好的Times New Roman斜体
plt.xlabel("Displacement ($\\textit{\\AA}$)", fontsize=12)

plt.show()

原理说明

  • mathtext.fontset = 'custom' 开启自定义数学字体配置
  • mathtext.rm 和 mathtext.it 分别指定数学模式下的罗马体和斜体为Times New Roman
  • 全局font.family设置确保非数学模式的文本(即"Displacement")也使用Times New Roman
  • 标签中的$\\textit{\\AA}$会触发数学模式的斜体渲染,此时会使用我们配置的Times New Roman斜体字体

替代方案(依赖LaTeX环境)

如果本地安装了LaTeX环境,也可以开启usetex直接渲染样式:

import matplotlib.pyplot as plt

plt.rcParams['text.usetex'] = True
plt.rcParams['font.family'] = 'Times New Roman'

plt.xlabel(r"Displacement \textit{\AA}", fontsize=12)
plt.show()

内容的提问来源于stack exchange,提问作者Huhu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 17:50:43