我应该如何运行以下示例代码来
/pgfplots/legend to name={<name>}
将共享图例放在一组水平对齐的图形下方?
/pgfplots/legend to name={<name>}
手册中“轴外的图例”下使用的示例代码pgfplots
如下(前两行是我自己添加的)。
\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}
它应该生成共享图例以下一组三个水平对齐的图形:
然而,它会抛出错误
Something is wrong -- perhaps a missing \item
,并生成一个共享的图例向右。
编译器将此归咎于begin{center} end{center}
环境。但是,注释掉它们却没有帮助。
答案1
下列的答案由@LaRiFaRi 提供,groupplots
对我有用(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}