# PGFplot to occupy full \linewidth, and aligned y=axis across tikzpicture

How do I get the tikzpicture to occupy the full \linewidth, and also have the left and right axis aligned? I suspect that some \phantom spacing applied to the tick labels will do the trick, but don't know how to do that.

\documentclass{article}
\usepackage{pgfplots}

\pgfkeys{/pgfplots/MyAxisStyle/.style={xmin=0,xmax=5, ymin=0,ymax=3,height=6cm,width=\linewidth}}
\pgfkeys{/pgfplots/MyLineStyle/.style={samples=50, smooth, ultra thick}}

\begin{document}

\begin{minipage}{0.9\linewidth}\centering
\hrule% To see actual \linewidth
\medskip
\begin{tikzpicture}
\begin{axis}[MyAxisStyle, xmax=10, ymax=110]
\addplot[MyLineStyle, domain=0:10, ,red] {(x^2)} node [left] {$y=x^2$};
\end{axis}
\end{tikzpicture}
\end{minipage}

\begin{minipage}{0.9\linewidth}\centering
\begin{tikzpicture}
\begin{axis}[MyAxisStyle, xmax=2,ymax=9]
\addplot[MyLineStyle, domain=0:2 ,green] {(x)^3} node [left] {$y=x^3$};
\end{axis}
\end{tikzpicture}
\end{minipage}

\begin{minipage}{0.9\linewidth}\centering
\begin{tikzpicture}
\begin{axis}[MyAxisStyle, width=0.5\linewidth]
\addplot[MyLineStyle, domain=0:5, blue] {sqrt(x)} node [above left] {$y=\sqrt{x}$};
\end{axis}
\end{tikzpicture}\hfill
\begin{tikzpicture}
\begin{axis}[MyAxisStyle, height=4cm,width=0.5\linewidth]
\addplot[MyLineStyle, domain=0:5, blue] {sqrt(x)} node [above left] {$y=\sqrt{x}$};
\end{axis}
\end{tikzpicture}
\hrule% To see actual \linewidth
\end{minipage}
\end{document}


Note that in graphs 1 and 2 the y-axis are not aligned, and that they don't quite occupy the full \linewidth as shown by the horizontal rule.

Also, on the last two graphs I applied width=0.5\linewidth, and expected that the \hfill should have no effect, but end up with significant horizontal space in between the two graphs.

I am not using the figure environment as I don't want to have these float, so instead I use a \minipage and apply \centering.

I don't want to use Scale TikZ figure to linewidth when relative positioning used as that will scale the text as well nor do I want to scale the picture as in How to scale a tikzpicture to \textwidth, but rather get the graph to use the full width

PGFplots has to estimate the space taken up by the labels, which is not easy and can lead to the plot having a different size than the one specified using width. In this case, I would use the option scale only axis, which applies the width and height options only to the axis, not the whole plot including the labels.

While this means you have to manually choose a width that's a bit smaller than your overall intended plot width, it makes the alignment of the plots a lot easier, since you now know that both large plot axes have the same width.

To align the different plots, you should keep them in the same tikzpicture, name the plots using name, and position them using at=(<other plot name>.below south east), anchor=(north east) or something similar. The anchors south, south east, etc. refer to points on the axes, the anchors below south east, right of south east, etc. refer to projections of points on the axes onto the bounding box of the plot.

\documentclass{article}
\usepackage{pgfplots}

\pgfkeys{/pgfplots/MyAxisStyle/.style={xmin=0,xmax=5, ymin=0,ymax=3,height=4cm,width=0.9\linewidth,scale only axis}}
\pgfkeys{/pgfplots/MyLineStyle/.style={samples=50, smooth, ultra thick}}

\begin{document}

\begin{minipage}{0.9\linewidth}\centering
\hrule% To see actual \linewidth
\medskip
\begin{tikzpicture}
\begin{axis}[MyAxisStyle, xmax=10, ymax=110,scale only axis,name=first]
\addplot[MyLineStyle, domain=0:10, ,red] {(x^2)} node [left] {$y=x^2$};
\end{axis}

\begin{axis}[MyAxisStyle, xmax=2,ymax=9,
at=(first.below south west),
anchor=north west,
yshift=-0.25cm,
name=second]
\addplot[MyLineStyle, domain=0:2 ,green] {(x)^3} node [left] {$y=x^3$};
\end{axis}

\begin{axis}[MyAxisStyle,
width=0.4\linewidth,
at=(second.below south west),
anchor=north west,
yshift=-0.25cm,
name=third]
\addplot[MyLineStyle, domain=0:5, blue] {sqrt(x)} node [above left] {$y=\sqrt{x}$};
\end{axis}
\begin{axis}[MyAxisStyle, height=3cm,width=0.4\linewidth,
at=(second.east|-third.outer south east),
anchor=outer south east
]
\addplot[MyLineStyle, domain=0:5, blue] {sqrt(x)} node [above left] {$y=\sqrt{x}$};
\end{axis}
\end{tikzpicture}
\hrule% To see actual \linewidth
\end{minipage}
\end{document}

• This works well. I'd prefer to have separate tikzpicture environment, so is there a way to apply a format to the axis labels so that they are typeset within a given space so that they occupy the same width. I could manually do that but would prefer that the specific tick labels are automatically selected. – Peter Grill Aug 4 '11 at 6:55
• You could set \pgfplotsset{yticklabel style={text width=3em,align=right}} to make all y labels take up the same amount of horizontal space, which will make the graphs align correctly on the left hand side if you remove the \centering (if you keep things centered, you also need to set the xticklabel style to a common width because the labels protrude on the right hand side of the graphs). This won't help in aligning things on the right hand side, though. – Jake Aug 4 '11 at 7:07
• I also prefer to use separate tikzpicture environments whenever I need more than one plot. Often, I excluded the axis descriptions from the bounding box using the trim features: this allows \centering if the left axis has its labels on its left and the right axis on its right - for all axes, independent of their label widths. The involved examples have made their way into the pgfplots manual, section "Alignment"... – Christian Feuersänger Aug 5 '11 at 15:07