matplotllib 图形大小和 xelatex 之间的长度比例不匹配

Length scale between matplotllib figure size and xelatex doesn't match

我用

创建了一个 python matplotlib
import matplotlib.pyplot as plt

xwidth = 418.25555 / 72 # conversion from in to pt
ywidth = 300 / 72 # conversion from in to pt

fig,ax = plt.subplots(figsize=[xwidth, ywidth])
ax.plot([1,2,3],[1,2,3])
ax.set_ylabel("Y-label")
ax.set_xlabel("X-label")
fig.savefig("test.pgf", bbox_inches="tight", pad=0)

我在 XeLaTeX 中通过 \the\textwidth 确定了 xwidth 并且必须将其转换为英寸。一英寸定义为 72 磅。但是,如果我将图形包含到我的文档中,它与线宽不匹配:

\documentclass{scrartcl}
\usepackage{pgf}
\begin{document}
  \begin{figure}
    \input{test.pgf}
  \end{figure}
  Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam\newline
  Linewidth: \the\textwidth
\end{document}

结果如下:

我做错了什么?

我认为,当您使用 bbox_inches="tight" 时,您会缩短图形以去除白色 space,并且不会延长轴。

使用 constrained_layout = trueplt.tight_layout() 对我来说在乳胶下工作。

import matplotlib.pyplot as plt

xwidth = 418.25555 / 72 # conversion from in to pt
ywidth = 300 / 72 # conversion from in to pt

fig,ax = plt.subplots(figsize=[xwidth, ywidth], constrained_layout = True)
ax.plot([1,2,3],[1,2,3])
ax.set_ylabel("Y-label")
ax.set_xlabel("X-label")
fig.savefig("test.pdf")

import matplotlib.pyplot as plt

xwidth = 418.25555 / 72 # conversion from in to pt
ywidth = 300 / 72 # conversion from in to pt

fig,ax = plt.subplots(figsize=[xwidth, ywidth])
ax.plot([1,2,3],[1,2,3])
ax.set_ylabel("Y-label")
ax.set_xlabel("X-label")
plt.tight_layout()
fig.savefig("test.pdf")