如何从组图中的不同图获取图例条目?

如何从组图中的不同图获取图例条目?

我想使用来自 中的多个图的符号在轴外放置图例groupplot。尝试下面显示的方式似乎不起作用,只显示最后一个条目。如何才能显示所有条目?

\documentclass[crop]{standalone}

\usepackage[svgnames]{xcolor}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\begin{document}

  \begin{tikzpicture}
    \begin{groupplot}[
      group style={
        {group size=2 by 2}},
      legend to name=grouplegend]

      \nextgroupplot[title=One]
      \addplot+[color=Blue] coordinates {(0,0) (1,1) (2,2)};
      \addlegendentry{A}

      \nextgroupplot[title=Two]
      \addplot+[color=Red] coordinates {(0,2) (1,1) (2,0)};
      \addlegendentry{B}

      \nextgroupplot[title=Three]
      \addplot+[color=Gray] coordinates {(0,2) (1,1) (2,1)};
      \addlegendentry{C}

      \nextgroupplot[title=Four]
      \addplot+[color=Orange] coordinates {(0,2) (1,1) (1,0)};
      \addlegendentry{D}

    \end{groupplot}

    \node (l1) at ($(group c1r2.south)!0.5!(group c2r2.south)$)
      [below, yshift=-2\pgfkeysvalueof{/pgfplots/every axis title shift}]
      {\ref{grouplegend}};

  \end{tikzpicture}

\end{document}

2x2 网格中的四个图仅显示最后一个条目的图例

答案1

您可以使用/pgfplots/legend to name={<name>}手册参考中“轴外的图例”下描述的功能。引用的示例为:

% Preamble: \pgfplotsset{width=7cm,compat=1.3}
\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}

其结果为:

一个传奇统治一切

编辑:我猜这对群体图不起作用。这里有一个糟糕的黑客方法,但还算管用:

\documentclass[crop]{standalone}

\usepackage[svgnames]{xcolor}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}

\begin{document}

  \begin{tikzpicture}
    \begin{groupplot}[
      group style={
        {group size=2 by 2}},
        xmin=0,
        enlarge x limits=true,
      ]

      \nextgroupplot[title=One, legend to name=grouplegend]
      \addplot+[color=Blue] coordinates {(0,0) (1,1) (2,2)};
      \addlegendentry{A}
      \addplot+[color=Red] coordinates {(-1,0)};
      \addlegendentry{B}
      \addplot+[color=Gray] coordinates {(-1,0)};
      \addlegendentry{C}
      \addplot+[color=Orange] coordinates {(-1,0)};
      \addlegendentry{D}

      \nextgroupplot[title=Two]
      \addplot+[color=Red] coordinates {(0,2) (1,1) (2,0)};

      \nextgroupplot[title=Three]
      \addplot+[color=Gray] coordinates {(0,2) (1,1) (2,1)};

      \nextgroupplot[title=Four]
      \addplot+[color=Orange] coordinates {(0,2) (1,1) (1,0)};

    \end{groupplot}

    \node (l1) at ($(group c1r2.south)!0.5!(group c2r2.south)$)
      [below, yshift=-2\pgfkeysvalueof{/pgfplots/every axis title shift}]
      {\ref{grouplegend}};

  \end{tikzpicture}

\end{document}

得出的结果为:

黑客来了

答案2

万一其他人遇到这种情况,在当前版本的 pgfplots (1.15) 中使用 groupplot、slurms 和 Carlisle 的答案确实有效。图中的示例(编辑:现在包括文档前言和用于编译的包装器):

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[utf8]{inputenc}

\usepackage{tikz} % To generate the plot from csv
\usepackage{pgfplots,pgfplotstable}
\usepgfplotslibrary{groupplots}

% Standard presentation for graphs
\pgfplotsset{compat=1.15} % Allows to place the legend below plot



\begin{document}

\begin{figure}[h!]
  \begin{center}


    \begin{tikzpicture}
    \begin{groupplot}[group style={
            group size=2 by 2,
            xticklabels at=edge bottom,
            yticklabels at=edge left
        }, 
        height=0.4\textwidth,
        width=0.5\textwidth,
        xmin=-5, xmax=55,
        ymin=70, ymax=105,
        xtick={0,10,30,50},
        ytick={70,80,90,100},
        ymajorgrids=true,
        grid style=dashed,
        legend to name=named,
        legend columns=-1
    ]

    \nextgroupplot
    \addplot coordinates {(0,98.65)(10,97.34)(30,91.23)(50,87.55)};
    \addplot coordinates {(0,98.15)(10,96.34)(30,90.23)(50,83.55)};

    \nextgroupplot
    \addplot coordinates {(0,98.65)(10,97.34)(30,91.23)(50,87.55)};
    \addplot coordinates {(0,98.15)(10,96.34)(30,90.23)(50,83.55)};    

    \nextgroupplot
    \addplot coordinates {(0,98.65)(10,97.34)(30,91.23)(50,87.55)};
    \addplot coordinates {(0,98.15)(10,96.34)(30,90.23)(50,83.55)};    

    \nextgroupplot
    \addplot coordinates {(0,98.65)(10,97.34)(30,91.23)(50,87.55)};
    \addplot coordinates {(0,98.15)(10,96.34)(30,90.23)(50,83.55)};


    \legend{Line 1, Line 2}

    \end{groupplot}
    \end{tikzpicture}

    \ref{named}

    \caption{My first groupplot plot.}


  \end{center}
\end{figure}

\end{document}

提供以下文档内容:

在此处输入图片描述

相关内容