为什么 siunitx LaTeX 包会为 matplotlib 图形中的某些文本元素添加垂直偏移?
Why does the siunitx LaTeX package add a vertical offset to some text elements in matplotlib figures?
我正在使用 LaTeX 在我的 matplotlib 图形中呈现文本。当我将包 siunitx 添加到我的 LaTeX 序言时,一些文本元素不再正确垂直对齐。
我最近才遇到这个问题:以前正确渲染的图形现在损坏了。它会影响多个 python 环境,所以我认为这可能是由于最新版本的 siunitx?
这是我的最小工作示例:
import numpy as np
import matplotlib.pyplot as plt
# Test data
x = np.linspace(0, 2*np.pi, 64)
y = np.sin(x)
# Use latex for text
plt.rcdefaults()
plt.rcParams['text.usetex'] = True
# Correctly behaving text
fig0, ax0 = plt.subplots()
ax0.plot(x, y, label='some string')
ax0.legend()
fig0.tight_layout()
fig0.savefig('without_siunitx.png')
plt.show()
# Use siunitx
plt.rcParams['text.latex.preamble'] = '\n'.join([
r'\usepackage{siunitx}',
])
# Now some text has incorrect y position
fig1, ax1 = plt.subplots()
ax1.plot(x, y, label='some string')
ax1.legend()
fig1.tight_layout()
fig1.savefig('with_siunitx.png')
plt.show()
对于最小工作示例,我在 Windows 10 上使用 python 3.8.12、matplotlib 3.3.2 和 siunitx 3.0.32(通过 MiKTeX 安装)。
生成的两张png图片链接如下:
使用siunitx后可以看到图例文字和y轴刻度标签错位
我将非常感谢任何能帮助我解决这个问题的人,因为它影响了我论文的大部分数据。非常感谢!
如果之前工作正常,您可以使用以下语法回滚到 siunitx 版本 2:
\usepackage{siunitx}[=v2]
我正在使用 LaTeX 在我的 matplotlib 图形中呈现文本。当我将包 siunitx 添加到我的 LaTeX 序言时,一些文本元素不再正确垂直对齐。
我最近才遇到这个问题:以前正确渲染的图形现在损坏了。它会影响多个 python 环境,所以我认为这可能是由于最新版本的 siunitx?
这是我的最小工作示例:
import numpy as np
import matplotlib.pyplot as plt
# Test data
x = np.linspace(0, 2*np.pi, 64)
y = np.sin(x)
# Use latex for text
plt.rcdefaults()
plt.rcParams['text.usetex'] = True
# Correctly behaving text
fig0, ax0 = plt.subplots()
ax0.plot(x, y, label='some string')
ax0.legend()
fig0.tight_layout()
fig0.savefig('without_siunitx.png')
plt.show()
# Use siunitx
plt.rcParams['text.latex.preamble'] = '\n'.join([
r'\usepackage{siunitx}',
])
# Now some text has incorrect y position
fig1, ax1 = plt.subplots()
ax1.plot(x, y, label='some string')
ax1.legend()
fig1.tight_layout()
fig1.savefig('with_siunitx.png')
plt.show()
对于最小工作示例,我在 Windows 10 上使用 python 3.8.12、matplotlib 3.3.2 和 siunitx 3.0.32(通过 MiKTeX 安装)。
生成的两张png图片链接如下:
使用siunitx后可以看到图例文字和y轴刻度标签错位
我将非常感谢任何能帮助我解决这个问题的人,因为它影响了我论文的大部分数据。非常感谢!
如果之前工作正常,您可以使用以下语法回滚到 siunitx 版本 2:
\usepackage{siunitx}[=v2]