[Tex/LaTex] Pgfplots: set plot margins

marginspgfplots

Is there a possibility in pgfplots to set the margins reserved for the x- and y-label, similarly to gnuplots tmargin, bmargin, lmargin, rmargin.

I would like to have following values for the horizontal direction.

lmargin: 0.3in
graphwidth=\columnwidth-0.4in
rmargin: 0.1in
plotwidth: \columnwidth (=picture width)

For the vertical direction:

bmargin: 0.3in
graphheight=4in-0.4in
tmargin: 0.1in
plotheight: 4in (=picture height)

Following minimal working example tries to scale only the axis (option scale only axis) and then trim with negative values accordingly.

MWE:

\documentclass[class=elsarticle,preprint,5p,twocolumn, 10pt]{standalone}
% NOTE: 'standalone' messes up \textheight,
% \textwidth and \columnwidth (252pt) are fine
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\pgfplotsset{compat=1.12,width=\columnwidth,height=4in}
\begin{document}
\begin{tikzpicture}[trim left=-0.3in, trim right=\columnwidth-0.3in]
% why is there no trim top or bottom?
\begin{axis}[width=\columnwidth-0.4in, height=4in-0.3in, scale only axis]
\addplot [red, mark=*] table {
0 1
2 5.5
3 7.25
4 8
};
\end{axis}
\end{tikzpicture}
\end{document}

Checking the pdf dimensions:

$ pdfinfo *.pdf | grep "Page size:"
Page size:      251.059 x 279.86 pts

The width is almost correct: 251.059pt instead of 252pt. Possibly numerical errors (only single precision). So I can live with this.

But there is no trim top and trim bottom. I couldn't find a workaround for this. Can anyone help me?

Possibly it is also a problem with standalone cropping. tikzscale is really slow, so I would prefer to avoid it. There should be a final picture which I can submit to a journal.

Best Answer

Set margins by adjusting the bounding box via \pgfresetboundingbox and scale only axis

Pgfplots manual describes adjusting the bounding box in Chapter 4.20.1 Bounding Box Restrictions (v1.12)

% Measures:
% ---------
% total width  = \columnwidth = 252pt = 3.49in
% total height = 4in = 288pt
% lmargin      = 0.4in
% rmargin      = 0.1in
% bmargin      = 0.4in
% tmargin      = 0.1in
% graph width  = \columnwidth - 0.5in
% graph height = 4in - 0.5in

MWE

\documentclass[class=elsarticle, preprint, 5p, twocolumn, 10pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{backgrounds}% required for 'inner frame sep' or 'tight background'
\pgfplotsset{compat=1.12}

\begin{document}
\begin{tikzpicture}[inner frame sep=0]
% 'tight background' is identical to 'inner frame sep=0'

  \begin{axis}[
      width=\columnwidth-0.5in,
      height=4in-0.5in,
      scale only axis,
      ylabel=Y-Value,
      xlabel=X-Value
    ]
    \addplot [red, mark=*] table {
    0 1
    2 5.5
    3 7.25
    4 8
    };
  \end{axis}

% Set margins by adjusting the bounding box:
\pgfresetboundingbox
\path
  (current axis.south west) -- ++(-0.4in,-0.4in)
  rectangle (current axis.north east) -- ++(0.1in,0.1in);
\end{tikzpicture}
\end{document}

Notes

  • \pgfresetboundingbox is incompatible with the external library
  • adjusting margins by inner frame xsep/ysep do not allow to set lmargin and rmargin independently (same for tmargin and bmargin) (suggested in the comment by @zeroth)

Measures confirmed by

$ pdfinfo *.pdf | grep "Page size:"
Page size:      251.055 x 287.996 pts