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

如何通过Matplotlib的rc库LaTeX功能设置XRD图正体θ轴标签?

Matplotlib绘制XRD图:正体θ的实现方案

针对开启LaTeX渲染后无法得到正体θ的问题,以下是几种可行解决方法:

方法1:用LaTeX的\textup命令强制正体

在x轴标签中用\textup{θ}包裹θ字符,直接实现正体效果。示例代码:

import matplotlib.pyplot as plt

# 配置LaTeX和衬线字体
plt.rcParams.update({
    "text.usetex": True,
    "font.family": "serif",
    "font.serif": ["Times New Roman"]  # 适配论文常用字体
})

fig, ax = plt.subplots()
# 设置正体2θ的x轴标签
ax.set_xlabel(r"2$\textup{θ}$")
# 模拟XRD测试数据
ax.plot([15, 25, 35, 45], [0.8, 2.5, 1.2, 0.3])
plt.show()

方法2:加载amsmath包使用\text命令

若\textup不生效,可在LaTeX预导言中引入amsmath包,再用\text{θ}实现正体。修改配置后的代码:

import matplotlib.pyplot as plt

plt.rcParams.update({
    "text.usetex": True,
    "font.family": "serif",
    "font.serif": ["Times New Roman"],
    "text.latex.preamble": r"\usepackage{amsmath}"  # 加载amsmath包
})

fig, ax = plt.subplots()
ax.set_xlabel(r"2$\text{θ}$")
ax.plot([15, 25, 35, 45], [0.8, 2.5, 1.2, 0.3])
plt.show()

方法3:直接使用Unicode正体θ字符

如果LaTeX命令始终存在兼容问题,可跳过LaTeX转义,直接使用Unicode正体θ字符(确保你的字体支持该字符,Times New Roman等学术字体均支持):

import matplotlib.pyplot as plt

plt.rcParams.update({
    "text.usetex": True,
    "font.family": "serif",
    "font.serif": ["Times New Roman"]
})

fig, ax = plt.subplots()
ax.set_xlabel("2θ")  # 直接使用正体θ字符
ax.plot([15, 25, 35, 45], [0.8, 2.5, 1.2, 0.3])
plt.show()

内容的提问来源于stack exchange,提问作者Philip de Bruin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 15:43:21