3

How should I run the following example code using /pgfplots/legend to name={<name>} to put a shared legend below a group of horizontally aligned figures?

The example code using /pgfplots/legend to name={<name>} in the pgfplots manual under "Legends outside of an axis" is as follows (the first two lines are added by myself).

\documentclass{standalone}
\usepackage{pgfplots}
% Preamble: \pgfplotsset{width=7cm,compat=1.3}
\begin{document}
\pgfplotsset{footnotesize,samples=10}
\begin{center}% note that \centering uses less vspace...
\begin{tikzpicture}
\begin{axis}[
legend columns=-1,
legend entries={$(x+0)^k$;,$(x+1)^k$;,$(x+2)^k$;,$(x+3)^k$},
legend to name=named,
title={$k=1$}]
\addplot {x};
\addplot {x+1};
\addplot {x+2};
\addplot {x+3};
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
\begin{axis}[title={$k=2$}]
\addplot {x^2};
\addplot {(x+1)^2};
\addplot {(x+2)^2};
\addplot {(x+3)^2};
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
\begin{axis}[title={$k=3$}]
\addplot {x^3};
\addplot {(x+1)^3};
\addplot {(x+2)^3};
\addplot {(x+3)^3};
\end{axis}
\end{tikzpicture}
\\

\ref{named}
\end{center}
\end{document}

It should generate a shared legend below a group of three horizontally aligned figures: legend in the below

However, it throws the error Something is wrong -- perhaps a missing \item, and generates a shared legend to the right. legend to the right

The compiler blames the begin{center} end{center} environment. However, commenting them out does not help.

7
  • Please make your code compilable. Do you really work in standalone?
    – LaRiFaRi
    Commented Jul 2, 2015 at 7:41
  • @LaRiFaRi It is not compilable itself. The code is copied from the pgfplots manual. I have only added the first two lines \documentclass{standalone} \usepackage{pgfplots}.
    – hengxin
    Commented Jul 2, 2015 at 7:45
  • At least surround it by \begin{document}\end{document}. I would do this with groupplots like I did here: tex.stackexchange.com/a/248151 When you are in standalone, the center makes no sense.
    – LaRiFaRi
    Commented Jul 2, 2015 at 7:48
  • @LaRiFaRi Sorry, \begin{document}\end{document} added. Thanks. I am using standalone because I want to generate a .pdf file and then import it into my main .tex file. I changed standalone to article, and it generated too much space around the figure.
    – hengxin
    Commented Jul 2, 2015 at 7:56
  • 1
    @LaRiFaRi Following the answer you mentioned, I have done it with groupplots. Thanks.
    – hengxin
    Commented Jul 2, 2015 at 9:49

1 Answer 1

7

Following the answer given by @LaRiFaRi, groupplots works for me (code at ShareLatex):

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}
\pgfplotsset{compat=1.3}
\begin{document}
  \begin{tikzpicture}
    \pgfplotsset{footnotesize,samples=10}
    \begin{groupplot}[group style = {group size = 3 by 1, horizontal sep = 50pt}, width = 6.0cm, height = 5.0cm]
        \nextgroupplot[ title = {$k=1$},
            legend style = { column sep = 10pt, legend columns = -1, legend to name = grouplegend,}]
            \addplot {x};   \addlegendentry{$(x+0)^k$}%
            \addplot {x+1}; \addlegendentry{$(x+1)^k$}
            \addplot {x+2}; \addlegendentry{$(x+2)^k$}
            \addplot {x+3}; \addlegendentry{$(x+3)^k$}
        \nextgroupplot[title = {$k=2$},]
            \addplot {x^2};
            \addplot {(x+1)^2};
            \addplot {(x+2)^2};
            \addplot {(x+3)^2};
        \nextgroupplot[title = {$k=3$},] 
            \addplot {x^3};
            \addplot {(x+1)^3};
            \addplot {(x+2)^3};
            \addplot {(x+3)^3};                    
    \end{groupplot}
    \node at ($(group c2r1) + (0,-4.0cm)$) {\ref{grouplegend}}; 
\end{tikzpicture}
\end{document}

Not the answer you're looking for? Browse other questions tagged .