0

I have two graphs that are next to each other in tikz, they are seperated by tabular.

The graphs show three lines each and the lines are of the same kind in both of the graphs. So I would like a joint legend, and not a legend for each of them.

How can I add a legend below the two figures, centered, and horizontal?

I saw several answers on stack: How to put legend below the chart? However, this puts the legend below a graph, and I have two graphs and it should be put centered below them and also horizontal.

Basically, all I want is a box in the form of:

------------------------------------------------------------------------------------
| <line1> Legend description <line2> Legend description <line3> Legend description |
------------------------------------------------------------------------------------

Where <line> is the style of the line. This box should be centered below the two graphs.

This is the code, removed the actual plots and some other things to keep it anonymous:

begin{figure}[!hbt]
\centering
\begin{tabular}{ll}
\resizebox{175pt}{!}{%
\begin{tikzpicture}
  \begin{axis}[
    xlabel=x,
    ylabel=y,
    grid=both,
    grid style={line width=.1pt, draw=gray!10},
    major grid style={line width=.2pt,draw=gray!50},
    axis lines=middle,
    minor tick num=5,]
    \addplot coordinates {};
    \addplot coordinates {};
    \addplot coordinates {};
  \end{axis}
\end{tikzpicture}
}
&
\resizebox{200pt}{!}{%
\begin{tikzpicture}
  \begin{axis}[
    xlabel=x,
    ylabel=y,
    grid=both,
    grid style={line width=.1pt, draw=gray!10},
    major grid style={line width=.2pt,draw=gray!50},
    axis lines=middle,
    minor tick num=5,]
    \addplot coordinates {};
    \addplot coordinates {};
    \addplot coordinates {};
  \end{axis}
\end{tikzpicture}
}
\end{tabular}

\caption{}
\label{}
\end{figure}
2
  • Are you using pgfplots or have you created the plots from scratch with pgf/tikz?
    – daniatic
    Feb 17 '18 at 11:23
  • Check my code I added
    – osk
    Feb 17 '18 at 11:24
1

You can use the legend to name key to get what you want. It is documented in the pgfplots manual in section 4.9.7 on page 265.

The following example is taken from the manual:

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

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.