我正在使用 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 軸刻度標簽未對齊。
I'll be really grateful to anybody who can help me solve this issue, as it affects most of the figures for my thesis. Thanks very much!
uj5u.com熱心網友回復:
如果之前運行正常,您可以使用以下語法回滾到 siunitx 的第 2 版:
\usepackage{siunitx}[=v2]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/328179.html
上一篇:如何在資料框中創建多個箱線圖?
