Similar to this question, I need output as follows but in a neat way. My solution code is not neat any more: I added a dummy axis to use its legend. Also the legend must be at top of the page.

Here its code:
\documentclass{article}
\usepackage{pgfplots}
\newcommand{\realplot}[1]{
\begin{tikzpicture}
\begin{axis}
\addlegendimage{empty legend}\addlegendentry{Matrix #1}
\addplot {0};
\addplot {1};
\addplot {2};
\addplot {3};
\end{axis}
\end{tikzpicture}}
\begin{document}
\begin{figure}[ht]
\realplot{1}\realplot{2}\realplot{3}
\realplot{4}\realplot{5}\realplot{6}
\begin{tikzpicture} % here starts the dummy tikzpicture
\begin{axis}[
legend columns=4,
legend style={%
at={(-100,-100)},%
anchor=north east,%
at={(axis description cs:1,10),%
},%
/tikz/column 2/.style={column sep=5pt,},},]
\addplot {0};
\addlegendentry{Method A}
\addplot {0};
\addlegendentry{Method B}
\addplot {0};
\addlegendentry{Method C}
\addplot {0};
\addlegendentry{Method D}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

/pgfplots/legend to name={<name>}approach Slurm mentions is the right way to go. Do you also need to "collect" legend entries from different axes, or do they all contain the same plot types? At the moment, it seems like you're misusing the legend in each plot for placing titles. Thetitlefunction is the correct way to go about this. – Jake Apr 25 '13 at 13:32