ju.
ju.

Reputation: 344

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

I created a python matplotlib figure with

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)

I determined the xwidth via \the\textwidth in XeLaTeX and have to convert it to inches. One inch is defined as 72 pt. However, if I include the graphic into my document, it doesn't match the linewidth:

\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}

Here is the result: Example of not matching graphic width

What am I doing wrong?

Upvotes: 0

Views: 166

Answers (1)

Liris
Liris

Reputation: 1511

I think that, when you use bbox_inches="tight", you shorten the figure to remove the white space, and do not extend the axes.

Using constrained_layout = true or plt.tight_layout() works under latex for me.

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")

or

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")

Upvotes: 1

Related Questions