# Global legend for multiple figures

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}
\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,},},]
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

• I don't entirely understand what you're trying to achieve here, or how it's different from the question you linked to. If all you want is a legend outside the axes, the /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. The title function is the correct way to go about this.
– Jake
Apr 25 '13 at 13:32

Since all axes contain same plot types, I have added a custom legend, which is taken from another answer.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfkeys}
\newcommand{\realplot}[1]{
\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}}
% argument #1: any options
\newenvironment{customlegend}[1][]{%
\begingroup
% inits/clears the lists (which might be populated from previous
% axes):
\csname pgfplots@init@cleared@structures\endcsname
\pgfplotsset{#1}%
}{%
% draws the legend:
\csname pgfplots@createlegend\endcsname
\endgroup
}%

% makes \addlegendimage available (typically only available within an
% axis environment):
\pgfplotsset{
cycle list={%
{draw=black,mark=star,solid},
{draw=black, mark=square,solid},%densely dashed},
{draw=black,mark=+,solid},%dashdotted}, %every mark/.append style={rotate=90},
{black,mark=o},}}
\begin{document}
\begin{figure}[ht]
\hspace{10ex}
\begin{tikzpicture}
\begin{customlegend}[legend columns=4,legend style={align=left,draw=none,column sep=2ex},legend entries={Method A ,Method B ,Method C,Method D}]