# Tikz axis width and height keys do not get correct aspect ratio

I have a problem when I try to set width and height of the axis environment in a tikzpicture. Consider the following MWE:

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=2,
ymin=0, ymax=3,
width=2\textwidth,
height=3\textwidth,
xmajorticks=false,
ymajorticks=false,
xmajorgrids,
ymajorgrids,
]
\end{axis}
\end{tikzpicture}

\end{document}


Why is it that with this code the output figure has indeed aspect ratio (close to) 3:2:

but if I change the lines

width=2\textwidth,
height=3\textwidth,


to

width=2\textwidth,
height=3\textwidth,


the output I get is skewed (the aspect ratio is something around 2.5:1) (which, btw, can also be seen by the fact that the grid lines no longer create squares but rectangles)

I tried reading the manual, but I didn't find anything (I probably didn't look for the right keyword).

What am I missing?

EDIT

sorry, I shouldn't post when it's this late. What I meant to write was: but if I change the lines

width=2\textwidth,
height=3\textwidth,


to

width=0.2\textwidth,
height=0.3\textwidth,


the output I get is skewed.

And actually, this applies to any other number, for that matter. If I keep the aspect ratio fixed but change the absolute values for width and height, the resulting picture will be more skewed the smaller the values I use

Sorry again for the troubles.

I found the solution. It was clearly stated in the pgfplots manual, it was just a matter of finding it:

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. That means that multiple pictures with the same target dimensions will have the same size for their axis boxes – even if the size for descriptions varies. It is also possible to scale the axis box to the prescribed width/height. In that case, the total width will be larger due to the axis descriptions. However, the axis box fills the desired dimensions exactly. [...] If scale only axis is enabled, width and height apply only to the axis rectangle. Consequently, the resulting figure is larger that width and height (because of any axis descriptions). However, the axis box has exactly the prescribed target dimensions. If scale only axis=false (the default), pgfplots will try to produce the desired width including labels, titles and ticks.

So, to have the proper aspect ratio for all sizes, simply add scale only axis to the option keys for the axis environment.

Assuming that you meant to write

"change width=2\textwidth, height=3\textwidth, to width=3\textwidth, height=2\textwidth"

then indeed your code produces rectangles with height over width ratios of 3^2/2^2=2.25, which is close to 2.5. However, once you also adjust xmax=3 and ymax=2, you're back to squares.

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=3,
ymin=0, ymax=2,
width=3\textwidth,
height=2\textwidth,
xmajorticks=false,
ymajorticks=false,
xmajorgrids,
ymajorgrids,
]
\end{axis}
\end{tikzpicture}

\end{document}


In both cases, TikZ does produce the appropriate aspect ratios.

UPDATE I confirm the feature described in the updated question in that there is something wrong for `width=0.2\textwidth, height=0.3\textwidth,'. Strictly speaking, this issue does not arise for all prefactors, but it starts at some critical width of the order 10cm and becomes worse if one goes to smaller widths. So the above answer is certainly irrelevant for the updated question.

• It's still wrong :) It is supposed to be TikZ or even TikZ. The capitalisation is inferred from the capitalisation of the German acronym (TikZ ist kein Zeichenprogramm). – Henri Menke Dec 13 '17 at 1:06
• sorry everyone, I though I proofread it enough, but it turns out I didn't. Please see the edit in my question – Mattia Tamellini Dec 13 '17 at 1:09
• @HenriMenke ThankZ! (BTW, what does the "T" in TikZ stand for? Don't tell me "TikZ" ;-) – user121799 Dec 13 '17 at 1:11
• I saw your update, thank you! Could it be a bug? Or maybe there is some "invisibile" elements in the plot taking up a fixed amount of space, which becomes more relevant for smaller figures. I can't see any other reason why this would work only for sizes greater than a threshold. I can work around it by trial-and-error, but it still seems a bizarre behaviour to me – Mattia Tamellini Dec 13 '17 at 9:57
• @MattiaTamellini Sorry, at this point I do not know how to turn this off. And I agree with you that it is odd. – user121799 Dec 13 '17 at 12:24