gpt4 book ai didi

python - 为什么 siunitx LaTeX 包会为 matplotlib 图形中的某些文本元素添加垂直偏移?

转载 作者:行者123 更新时间:2023-12-04 13:05:32 27 4
gpt4 key购买 nike

我正在使用 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图片链接如下:

Correctly aligned figure without siunitx

Incorrectly aligned figure with siunitx

使用siunitx后可以看到图例文字和y轴刻度标签错位。

我真的很感谢任何能帮助我解决这个问题的人,因为它影响了我论文的大部分数据。非常感谢!

最佳答案

如果之前工作正常,您可以使用以下语法回滚到 siunitx 版本 2:

\usepackage{siunitx}[=v2]

关于python - 为什么 siunitx LaTeX 包会为 matplotlib 图形中的某些文本元素添加垂直偏移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69645097/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com