7

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.

enter image description here

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}
  • 4
    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
6

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}
        \addlegendimage{empty legend}\addlegendentry{Matrix #1}
        \addplot {0};
        \addplot {1};
        \addplot {2};
        \addplot {3};
    \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):
    \def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}
\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}]
        \addlegendimage{mark=star,solid,line legend}
        \addlegendimage{mark=square,solid}   
        \addlegendimage{mark=+,solid}
        \addlegendimage{mark=o}
        \end{customlegend}
     \end{tikzpicture}

\realplot{1}\realplot{2}\realplot{3}
\realplot{4}\realplot{5}\realplot{6}
\end{figure}
\end{document}

Output:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.