# How to align PGFplot subplots

I am using the matlab2tikz MATLAB function to export a MATLAB figure with subplots to tikz code. Unfortunately the subplots do not align properly. The post PGFplot to occupy full \linewidth, and aligned y=axis across tikzpicture seems to address a similar issue. However, I looked at the code in the .tikz file, and it appears to use the method suggested in the answer to that post, but the plots are still not aligned correctly. Below is an image to show this with the corresponding code. Sorry it's a bit long, it was as minimal as I could make it but still reproduce the issue.

How can I get the plots to align properly?

\documentclass{article}

\usepackage{pgfplots}
\usetikzlibrary{external}
\tikzexternalize[prefix=compiled_figures/]
\pgfplotsset{try min ticks=3}
\pgfplotsset{max space between ticks=50pt}

\begin{document}
\begin{figure}[!htbp]
\centering
\newlength\figureheight
\newlength\figurewidth
\setlength\figureheight{3cm}
\setlength\figurewidth{2cm}
\tikzset{external/remake next}

\begin{tikzpicture}

\begin{axis}[%
width=\figurewidth,
height=\figureheight,
scale only axis,
xmin=0, xmax=3,
ymin=-0.5, ymax=1.5,
name=plot3,
title={$p = 2$}]
color=blue,
solid,
line width=2.0pt,
forget plot
]
coordinates{
(0,0.46)(0.25,0.80)(0.5,1.02)(0.75,1.27)(1,0.59)(1.25,0.15)(1.5,-0.02)(1.75,-0.27)(2,-0.05)(2.25,0.03)(2.5,0)(2.75,0)(3,0)
};
\end{axis}

\begin{axis}[%
width=\figurewidth,
height=\figureheight,
scale only axis,
xmin=0, xmax=1.5,
ymin=0, ymax=1,
ylabel={$\phi(t)$},
name=plot1,
at=(plot3.left of south west), anchor=right of south east,
title={$p = 1$}]
color=blue,
solid,
line width=2.0pt,
forget plot
]
coordinates{
(0,1)(0.25,1)(0.5,1)(0.75,1)(1,0)(1.25,0)(1.5,0)
};
\end{axis}

\begin{axis}[%
width=\figurewidth,
height=\figureheight,
scale only axis,
xmin=0, xmax=1,
ymin=-1, ymax=1,
ylabel={$\psi(t)$},
name=plot2,
at=(plot1.below south west), anchor=above north west]
color=blue,
solid,
line width=2.0pt,
forget plot
]
coordinates{
(0,1)(0.25,1)(0.5,-1)(0.75,-1)(1,0)
};
\end{axis}

\begin{axis}[%
width=\figurewidth,
height=\figureheight,
scale only axis,
xmin=-1, xmax=1.5,
ymin=-2, ymax=2,
name=plot4,
at=(plot2.right of south east), anchor=left of south west]
color=blue,
solid,
line width=2.0pt,
forget plot
]
coordinates{
(-1,-0.125)(-0.75,-0.217)(-0.5,-0.274)(-0.25,-0.341)(0,0.707)(0.25,1.457)(0.5,-0.091)(0.75,-1.02)(1,-0.216)(1.25,0.124)(1.5,0)
};
\end{axis}

\begin{axis}[%
width=\figurewidth,
height=\figureheight,
scale only axis,
xmin=-2, xmax=2,
ymin=-1, ymax=1.5,
name=plot6,
at=(plot4.right of south east), anchor=left of south west]
color=blue,
solid,
line width=2.0pt,
forget plot
]
coordinates{
(-2,0.023)(-1.75,0.056)(-1.5,0.089)(-1.25,0.128)(-1,-0.017)(-0.75,-0.238)(-0.5,-0.444)(-0.25,-0.699)(0,0.136)(0.25,1.416)(0.5,0.599)(0.75,-0.787)(1,-0.443)(1.25,0.146)(1.5,0.056)(1.75,-0.0234374999975261)(2,0)
};
\end{axis}

\begin{axis}[%
width=\figurewidth,
height=\figureheight,
scale only axis,
xmin=0, xmax=4.5,
ymin=-0.5, ymax=1.5,
at=(plot6.above north west), anchor=below south west,
title={$p = 3$}]
color=blue,
solid,
line width=2.0pt,
forget plot
]
coordinates{
(0,0.221)(0.25,0.536)(0.5,0.842)(0.75,1.212)(1,0.991)(1.25,0.547)(1.5,0.19)(1.75,-0.285)(2,-0.259)(2.25,-0.069)(2.5,-0.032)(2.75,0.070)(3,0.046)(3.25,-0.015)(3.5,-0.006)(3.75,0.002)(4,0)(4.25,0)(4.5,0)
};
\end{axis}
\end{tikzpicture}%

\end{figure}
\end{document}

-
–  Claudio Fiandrino Feb 21 '13 at 8:07

Groupplot seems to make the deal. As you can read here : How to stack and align multiple plots with same x but different y axes? Notice that you should adjust the vertical set parameter in order to have some gap, as well as group size.

This would be something like this :

\documentclass{article}

\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}

\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group name=my plots,
group size=3 by 3,
ylabels at=edge left
},
footnotesize,
width=5cm,
height=4cm,
tickpos=left,
ytick align=outside,
xtick align=outside,
enlarge x limits=false
]

\nextgroupplot[title={$p=1$},ylabel=$\phi(t)$]
(0,1)(0.25,1)(0.5,1)(0.75,1)(1,0)(1.25,0)(1.5,0)
};
\nextgroupplot[title={$p=2$}]
(0,1)(0.25,1)(0.5,-1)(0.75,-1)(1,0)
};
\nextgroupplot[title={$p=3$}]
coordinates{
(-1,-0.125)(-0.75,-0.217)(-0.5,-0.274)(-0.25,-0.341)(0,0.707)(0.25,1.457)(0.5,-0.091)(0.75,-1.02)(1,-0.216)(1.25,0.124)(1.5,0)
};
\nextgroupplot[ylabel=$\psi(t)$]
coordinates{
(-1,-0.125)(-0.75,-0.217)(-0.5,-0.274)(-0.25,-0.341)(0,0.707)(0.25,1.457)(0.5,-0.091)(0.75,-1.02)(1,-0.216)(1.25,0.124)(1.5,0)
};
\nextgroupplot
coordinates{
(-2,0.023)(-1.75,0.056)(-1.5,0.089)(-1.25,0.128)(-1,-0.017)(-0.75,-0.238)(-0.5,-0.444)(-0.25,-0.699)(0,0.136)(0.25,1.416)(0.5,0.599)(0.75,-0.787)(1,-0.443)(1.25,0.146)(1.5,0.056)(1.75,-0.0234374999975261)(2,0)
};
\nextgroupplot
coordinates{
(0,0.221)(0.25,0.536)(0.5,0.842)(0.75,1.212)(1,0.991)(1.25,0.547)(1.5,0.19)(1.75,-0.285)(2,-0.259)(2.25,-0.069)(2.5,-0.032)(2.75,0.070)(3,0.046)(3.25,-0.015)(3.5,-0.006)(3.75,0.002)(4,0)(4.25,0)(4.5,0)
};
\end{groupplot}
\end{tikzpicture}

\end{document}


Of course your plot options can be added as well (i kept only solid to make code shorter)...

-
This works, thanks so much! –  user25149 Feb 21 '13 at 14:19
ok mistakes corrected, Jake. –  s__C Feb 21 '13 at 17:24
@user25149: If you consider your question answered, you can mark it as such by clicking the tick mark next to s__C's answer –  Jake Feb 21 '13 at 17:38