如同这个问题,我需要输出如下但要简洁。我的解决方案代码不再简洁:我添加了一个虚拟代码axis
来使用其图例。此外,图例必须位于页面顶部。
这是它的代码:
\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}
答案1
由于所有轴都包含相同的绘图类型,因此我添加了自定义图例,该图例取自另一个回答。
\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}
输出: