1

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!

3 Answers 3

2

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

4
  • +1: Ther answers, that's what I call a great service! Nov 4, 2021 at 1:45
  • @Dr. Manuel Kuehner Thank you very much.
    – Roland
    Nov 4, 2021 at 2:04
  • Thanks! Is there a way to set the legend location without manually specifying x and y coordinates through trail and errror? Perhaps it is possible to draw a line through the corners of the tikzpicture, then you can set the point where these intersect as a the x coordinate and y to 0, or something like that?
    – Kyle
    Nov 4, 2021 at 12:22
  • @Kyle Yes, I changed it see edit.
    – Roland
    Nov 4, 2021 at 22:33
2

Another posibility is manually add legend entries with \addlegendimage{green} and \addlegendentry{$P3$} to have the same effect.

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

\pgfplotsset{
    legend image with text/.style={
        legend image code/.code={%
            \node[anchor=center] at (0.3cm,0cm) {#1};
        }
    },
}

\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,legend style={at={(0.88,0.90)},anchor=north},
    legend cell align={left}]
\addplot[color=red]{x^2 - 1};\label{pgfplots:p1} % plot two different functions
\addplot[color=blue]{e^x};\label{pgfplots:p2}
\addlegendentry{$P1$}
\addlegendentry{$P2$}
\addlegendimage{green}
\addlegendentry{$P3$}

\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}

enter image description here

Another Version with groupplots:

\documentclass{article}
\usepackage[margin=0.25in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\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 at (9,0)
    [below, yshift=-2\pgfkeysvalueof{/pgfplots/every axis title shift}]
    {\ref{grouplegend}};

\end{tikzpicture}

\end{document}

enter image description here

1

You could draw the legend like normal with \legend{P1,P2,P3} put die plot P3 (\addplot[color=green{e^-x};) in the first plot and just don not draw it with draw=none in the optinal argument. The plot itself will dissapear while the entry in the legend remains.

\documentclass{article}
\usepackage[margin=0.25in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{pgfplots}
\usepgfplotslibrary{external} 
\usetikzlibrary{calc} 
\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,legend style={at={(0.88,0.90)},anchor=north},
    legend cell align={left}]
\addplot[color=red]{x^2 - 1};\label{pgfplots:p1} % plot two different functions
\addplot[color=blue]{e^x};\label{pgfplots:p2}
\addplot[color=green,draw=none]{e^-x};\label{pgfplots:p3}
\legend{P1,P2,P3}
\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}

enter image description here

Another Version with grouplots:

\documentclass{article}
\usepackage[margin=0.25in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\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$}
        \addplot[color=green,draw=none]{e^-x};\label{pgfplots:p3}
        \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 at (9,0)
    [below, yshift=-2\pgfkeysvalueof{/pgfplots/every axis title shift}]
    {\ref{grouplegend}};

\end{tikzpicture}

\end{document}

enter image description here

1
  • Thanks! The only problem is that doing addplot with draw=none for many more plots than this could be a pain. It would be ideal if there is a way to do this without having to manually add each unique plot again to the axes environment of choice. In my actual case I have 8 subplots, so I would need a way to ensure the legend is big enough and outside the plot axes.
    – Kyle
    Nov 3, 2021 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.