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

Matplotlib轴标签$$格式化丢失Barlow字体问题求助

解决Matplotlib中TeX格式轴标签无法应用自定义字体的问题

问题背景

作为具备基础Python能力的桥梁工程师,在撰写博士论文使用Matplotlib绘图时遇到以下问题:

  • 需要绘制的轴标签要求:f斜体、cm下标、[MPa]正常文本
  • 设置了Barlow字体后,用$包裹的TeX格式部分(如$\it{f}_{cm}$)无法应用该字体,仍显示默认字体
  • 尝试过原始字符串等方法无效,不想使用复杂的LaTeX环境

附原代码:

import matplotlib.pyplot as plt

# Figure dimensions:
cm = 1 / 2.54
height = 7
width = 12
dpi = 300
plt.figure(figsize=(round(width * cm, 6), round(height * cm, 6)), dpi=dpi)
ax = plt.axes()

plt.subplots_adjust(left=0.0625, right=0.85, top=0.9286, bottom=0.1429)

# Data:
x_list = [30, 50, 60, 80]
y_list = [24, 26, 30, 34]
plt.plot(x_list, y_list, linewidth=0, marker='o', markersize=2.5)

#Axis label:
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

ax.set(xlim=(25, 85), ylim=(20, 38))

x_labels = [i for i in range(30, 85, 10)]
y_labels = [i for i in range(25, 38, 5)]

ax.set_xticks(x_labels)
ax.set_xticklabels(x_labels, font='Barlow', fontsize=8, va='center')
ax.xaxis.set_tick_params(bottom=False, pad=5)

ax.set_yticks(y_labels)
ax.set_yticklabels(y_labels, font='Barlow', fontsize=8, ha='right')
ax.yaxis.set_tick_params(bottom=False, pad=0)

ax.set_xlabel('$\it{f}_{cm}$ [MPa]', font='Barlow', size=9, ha='left', va='center')
ax.xaxis.set_label_coords(1.0256, 0)

plt.grid(visible=True, which='major', axis='both', color='#7F7F7F', linewidth=0.2, linestyle=(0, (10, 10)))

plt.show()

简单解决方案

核心原因:Matplotlib的内置TeX渲染(即使是inline模式)会使用LaTeX的默认字体,忽略你设置的Barlow字体。直接用Matplotlib原生的文本格式化语法,就能让整个标签统一应用自定义字体。

修改步骤:

  • 去掉$包裹,改用Matplotlib支持的原生标记:
    • \it 表示斜体
    • _{} 表示下标
  • 用原始字符串(加r前缀)避免转义问题

修改后的轴标签代码:

ax.set_xlabel(r'\it{f}_{cm} [MPa]', font='Barlow', size=9, ha='left', va='center')

完整修改后代码

import matplotlib.pyplot as plt

# Figure dimensions:
cm = 1 / 2.54
height = 7
width = 12
dpi = 300
plt.figure(figsize=(round(width * cm, 6), round(height * cm, 6)), dpi=dpi)
ax = plt.axes()

plt.subplots_adjust(left=0.0625, right=0.85, top=0.9286, bottom=0.1429)

# Data:
x_list = [30, 50, 60, 80]
y_list = [24, 26, 30, 34]
plt.plot(x_list, y_list, linewidth=0, marker='o', markersize=2.5)

#Axis label:
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

ax.set(xlim=(25, 85), ylim=(20, 38))

x_labels = [i for i in range(30, 85, 10)]
y_labels = [i for i in range(25, 38, 5)]

ax.set_xticks(x_labels)
ax.set_xticklabels(x_labels, font='Barlow', fontsize=8, va='center')
ax.xaxis.set_tick_params(bottom=False, pad=5)

ax.set_yticks(y_labels)
ax.set_yticklabels(y_labels, font='Barlow', fontsize=8, ha='right')
ax.yaxis.set_tick_params(bottom=False, pad=0)

# 关键修改:使用Matplotlib原生文本格式,去掉$,加r前缀
ax.set_xlabel(r'\it{f}_{cm} [MPa]', font='Barlow', size=9, ha='left', va='center')
ax.xaxis.set_label_coords(1.0256, 0)

plt.grid(visible=True, which='major', axis='both', color='#7F7F7F', linewidth=0.2, linestyle=(0, (10, 10)))

plt.show()

额外说明

如果你的系统中没有安装Barlow字体,需要先安装,或者用matplotlib.font_manager.FontProperties手动加载字体文件,确保Matplotlib能找到该字体。

内容的提问来源于stack exchange,提问作者Jakub Krząkała

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 11:07:08