首页 > 解决方案 > matplotllib 图形大小和 xelatex 之间的长度比例不匹配

问题描述

我创建了一个 pythonmatplotlib

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 中确定了xwidth通孔,必须将其转换为英寸。\the\textwidth一英寸定义为 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}

结果如下: 图形宽度不匹配的示例

我究竟做错了什么?

标签: pythonmatplotliblatexxelatexpgf

解决方案


我认为,当您使用 时bbox_inches="tight",您会缩短图形以删除空白,并且不要扩展轴。

为我使用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")

推荐阅读