3

EDIT: I completely rephrase the question. Consider the following MWE:

\documentclass{article}
\usepackage{showframe}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[height=\textheight,width=\textwidth,scale only axis]
    \addplot {2^x};
\end{axis}
\end{tikzpicture}
\end{document}

with the key scale only axis, you can see that both axes (x and y) have exactly the dimensions of my text page and labels etc, don't fit in there anymore. If I remove it, the axis environment gets scaled down in both dimensions so that everything fits on the page.

Now my question is if I can apply that key scale only axis only to the y axis. For example, because I want the plot to take the maximum width in x direction without going over the margins (so x dimension without scale only axis) but the axis to have an exact height of, say 5cm (so the option scale only axis would be needed for y).

In order to check if it is done right, I would like to have the example above where to one dimension the key is applied and to the other not.


OLD (original) QUESTION:

in pgfplots, is it possible to apply the key scale only axis only to the y axis? The reason why I ask is that I would like to give the overall width of the plot (including labels, etc), but need the axis to be exactly 5cm high.

  • give both width=x cm and height=5 cm keys – percusse Mar 30 '16 at 14:53
  • you probably misunderstood me: I would like the behaviour with scale only axis for the y axis and the behaviour without for the x axis. Is this possible? – riddleculous Mar 30 '16 at 15:09
  • Yes, also possible but can you include an example? scale only axis can give different results. Try \begin{axis}[height=5cm,width=4cm,scale only axis,]\addplot {2^x};\end{axis} which is what you want. – percusse Mar 30 '16 at 15:44
  • 2
    @riddleculous: The difference between scale only axis=true and scale only axis=false is a fixed length of 45pt: PGFPlots doesn't actually take into account how much space the labels and titles take up. So the (somewhat disenchanting) answer to your question is: use width=\textwidth, height=5cm+45pt or (equivalently) scale only axis, width=\textwidth-45pt, height=5cm – Jake Mar 30 '16 at 20:22
  • 2
    @riddleculous: A couple of paragraphs above, it says "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. However, the fixed amount is always the same; it is set to 45pt". There's a more involved approach using th tikzscale package that originated at tex.stackexchange.com/questions/36297/… – Jake Mar 31 '16 at 7:11
2

As Jake pointed out in his comments #1 and #2, the space considered to be used by the labels is always a fixed value of 45pt, see the PGFplots manual:

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. However, the fixed amount is always the same; it is set to 45pt

He also mentioned two equivalent possibilities which will yield the same result which is close to the actually desired: width=\textwidth, height=5cm+45pt or scale only axis, width=\textwidth-45pt, height=5cm

A more sophisticated approach is using the tikzscale package which was developed in order to overcome that drawback.

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.