Global Legend for Different Plots

legendpgfplotstikz-pgf

I have several axis environments that form an array of plots. Sometimes these different axes environments have the same data plotted, other times they have different data sets plotted. I would like to set a global legend at the bottom of all the N subplots that has one legend entry for each environment that has a unique set of data. I have simplified my problem to the following two subplots using functions instead of tabulated data. In this example, there are two data sets plotted in each of the subplots. In the first subplot, there are two unique plots (labelled p1 and p2). In the second subplot, one of the plots (the one also in red) is the same as p1, however the second plot is unique to any of the plots in the previous subplots. I would like an elegent way of defining one legend, with one entry for each of these plots labelled p1 through p3.

\documentclass{article}
\usepackage[margin=0.25in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{pgfplots}
\usepgfplotslibrary{external} 
\pgfplotsset{width=10cm,
            compat=1.9,
            }

\tikzexternalize

\begin{document}



\begin{tikzpicture}

\pgfplotsset{
            every axis plot/.style={line width=2.5pt},
            every axis post/.append style={xmin=-10, xmax=10}}

\begin{axis}[name=t1, height=5cm, width=10cm]
\addplot[color=red]{x^2 - 1};\label{pgfplots:p1} % plot two different functions
\addplot[color=blue]{e^x};\label{pgfplots:p2}
\end{axis}

\begin{axis}[name=t1a, at={($(t1.east)+(1cm,0cm)$)}, anchor=west, height=5cm, width=10cm]
\addplot[color=red]{x^2 - 1}; % same as p1
\addplot[color=green]{e^-x};\label{pgfplots:p3} % different to p1 & p2
\end{axis}

\end{tikzpicture}

\end{document}

There must be a way to define some legend and use \ref{plotname} to reference each of the unique lines or something. Thanks for any help!

Best Answer

A last and most simple solution is to just put the reference plot into a node with \node[yshift=-1cm] at (9,0) {\ref{pgfplots:p1} P1 \\ \ref{pgfplots:p2} P2 \ref{pgfplots:p3} P3}; in a grouplot.

\documentclass{article}
\usepackage[margin=0.25in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{pgfplots}
\usetikzlibrary{calc} 
\usepgfplotslibrary{groupplots}
\pgfplotsset{width=10cm,
            compat=1.9,
            }

\begin{document}

\begin{tikzpicture}

\pgfplotsset{
            every axis plot/.style={line width=2.5pt},
            every axis post/.append style={xmin=-10, xmax=10}}

\begin{groupplot}[group style={
        {group size=2 by 2}},name=t1, height=5cm, width=10cm,legend style={
        transpose legend,
        legend columns=0,
        draw=none }]
    \nextgroupplot[title=One, legend to name=grouplegend]
        \addplot[color=red]{x^2 - 1};\label{pgfplots:p1}
        \addlegendentry{$P1$}
        \addplot[color=blue]{e^x};\label{pgfplots:p2}
        \addlegendentry{$P2$}
        \addlegendimage{green}
        \addlegendentry{$P3$}

    \nextgroupplot[title=two]
        \addplot[color=red]{x^2 - 1}; % same as p1
        \addplot[color=green]{e^-x};\label{pgfplots:p3} % different to p1 & p2
    
\end{groupplot}

\node[yshift=-20pt] at ($(group c1r1.south)!0.5!(group c2r1.south)$) {\ref{pgfplots:p1} P1 \\ \ref{pgfplots:p2} P2 \ref{pgfplots:p3} P3};

\end{tikzpicture}

\end{document}

EDIT: \node[yshift=-20pt] at ($(group c1r1.south)!0.5!(group c2r1.south)$) {\ref{pgfplots:p1} P1 \\ \ref{pgfplots:p2} P2 \ref{pgfplots:p3} P3};

Results in:

enter image description here