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

Wasserwaage
  • 233
  • 3
  • 12
  • I generally use adjustbox for plot and legend (if outside of the plot, normally below the plot) separately to manage size/scale. – Dr.PB Sep 03 '20 at 09: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. – John Kormylo Sep 03 '20 at 13:21

1 Answers1

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}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • 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…? – Wasserwaage Sep 04 '20 at 06:51
  • Anything which does not affect the size of the plot can be left out. Otherwise, you really need the whole thing. – John Kormylo Sep 04 '20 at 14:19