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?
Thanks in advance!

\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$}]
\addplot [
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$}]
\addplot [
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]
\addplot [
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]
\addplot [
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]
\addplot [
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$}]
\addplot [
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}
