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}

结果如下:

我究竟做错了什么?


智慧大石
浏览 107回答 1
1回答

杨__羊羊

我认为,当您使用 时bbox_inches="tight",您会缩短图形以删除空白,并且不会延长轴。对我来说使用constrained_layout = true或plt.tight_layout()在乳胶下工作。import matplotlib.pyplot as pltxwidth = 418.25555 / 72 # conversion from in to ptywidth = 300 / 72 # conversion from in to ptfig,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 pltxwidth = 418.25555 / 72 # conversion from in to ptywidth = 300 / 72 # conversion from in to ptfig,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")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python