1

How can I create a standalone pgfplots figure with set total dimensions, including axis labels and legend entries?

If I set plot dimensions with width=5cm, height=5cm, pgfplots only gives me a total figure approximately this size. Additionally, if a legend is placed outside the axis box, the legend dimensions are added to the specified dimensions. The manual specifies (4.10.1 Common Scaling Options):

Please note that pgfplots only estimates the size needed for axis- and tick labels. The estimate assumes a fixed amount of space for anything which is outside of the axis box. This has the effect that the final images may be slightly larger or slightly smaller than the prescribed dimensions.

and further

If scale only axis=false (the default), pgfplots will try to produce the desired width including labels, titles and ticks.

My MWE:

\documentclass[crop]{standalone}

\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}

\begin{axis}[
%Grouping
    scale only axis=false,
    height=,
    width=10cm,
%Domain
    xmin=2000, xmax=2020,
    ymode=log,
%Ticks
    /pgf/number format/1000 sep={},
%Axis Labels
    ylabel= Price \text{[}\$(2020)/kg\text{]},
%Legend
    legend cell align={left},
    legend pos=outer north east,
    legend style={draw=none},
    legend entries={Entry},
]
\addplot[draw=none] coordinates {(2005,10000)};
\end{axis}

\end{tikzpicture}
\end{document}

Below is the visual representation of the solution posted by John Kormylo:

enter image description here

2
  • I generally use adjustbox for plot and legend (if outside of the plot, normally below the plot) separately to manage size/scale.
    – Mr.EU
    Sep 3 '20 at 9:25
  • There is no telling how big the legend is going to be, so it is not included in the scale calculations. You could do a trial run in a savebox and compensate. Sep 3 '20 at 13:21
2

It looks awful, but it does fit precisely into a 5cm by 5cm rectangle.

demo

\documentclass[crop]{standalone}

\usepackage[utf8]{inputenc}
\usepackage{tikz}% reducndant
\usepackage{pgfplots}

\newsavebox{\temp}

\begin{document}
\savebox{\temp}{\begin{tikzpicture}

\begin{axis}[
%Grouping
    name=border,
    height=5cm,
    width=5cm,
%Domain
    xmin=2000, xmax=2020,
    ymode=log,
%Ticks
    /pgf/number format/1000 sep={},
%Axis Labels
    ylabel= Price \lbrack\$(2020)/kg\rbrack,
%Legend
    legend cell align={left},
    legend pos=outer north east,
    legend style={draw=none},
    legend entries={Entry},
]
\addplot[draw=none] coordinates {(2005,10000)};
\end{axis}

\end{tikzpicture}}% trial

\begin{tikzpicture}

\begin{axis}[
%Grouping
    name=border,
    height={\dimexpr 10cm-\ht\temp},
    width={\dimexpr 10cm-\wd\temp},
%Domain
    xmin=2000, xmax=2020,
    ymode=log,
%Ticks
    /pgf/number format/1000 sep={},
%Axis Labels
    ylabel= Price \lbrack\$(2020)/kg\rbrack,
%Legend
    legend cell align={left},
    legend pos=outer north east,
    legend style={draw=none},
    legend entries={Entry},
]
\addplot[draw=none] coordinates {(2005,10000)};
\end{axis}
\draw[red] (current bounding box.south west) rectangle (current bounding box.north east);
\draw[green] (current bounding box.south west) rectangle ++(5cm,5cm);

\end{tikzpicture}

\end{document}
2
  • Neat! I added a visual representation to my question to make it easier to see your solution. I wonder if this could also be done without the need to have the entire figure in a temporary box…? Sep 4 '20 at 6:51
  • Anything which does not affect the size of the plot can be left out. Otherwise, you really need the whole thing. Sep 4 '20 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.