matplotlib tex 标签添加/移除空格

0 投票
1 回答
2440 浏览
提问于 2025-04-18 00:50

我有一个图表,y轴的标签上有一个上标,用来表示数量级。我是通过下面这行代码来实现的:

# plt is acquired from matplotlib.pyplot
# orderMagnitude is a variable taken from a different part of the code
plt.ylabel('${\mathrm{Intensity (10^'+str(int(orderMagnitude))+')}}$')

这里的{\mathrm{..}}部分是加上去的,因为如果不加的话,整个文本都会变成斜体(除了实际的数字)。不过现在这行代码似乎把y(表示强度的那个字母)和开括号之间的空格去掉了,同时在数量级的后面又多加了一些空格(见图片)。

enter image description here

有没有人知道这是为什么呢?

1 个回答

1

你不需要把整行都放在数学模式里,这正是你遇到问题的地方。特别是,你把整个公式都放在了 \mathrm{} 里,而不是仅仅放 {Intensity }。下面的写法可能会更好,这样你就不会在最后出现多余的空白了:

plt.ylabel('Intensity $(10^{' + str(int(orderMagnitude)) + '})$')

撰写回答