3

Assumed we have the following MWE to display various graphs in one single diagram.


Minimum working example (MWE):

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=newest, scale only axis}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
            axis y line*                                = left,
            legend style                                = {at={(0.5,-0.125)},
                anchor                                  = north,
                legend columns                          = 3,
                /tikz/every even column/.append style   = {column sep=0.5cm},
                /tikz/every odd column/.append style    = {column sep=0.15cm},
                },
            ]%
            \addplot coordinates {(0,0) (1,1)};
            \legend{Graph 1};
\end{axis}%
%
\begin{axis}[
            axis x line                                 = none,
            axis y line*                                = right,
            legend style                                = {at={(0.5,-0.125)},
                anchor                                  = north,
                legend columns                          = 3,
                /tikz/every even column/.append style   = {column sep=0.5cm},
                /tikz/every odd column/.append style    = {column sep=0.15cm},
            },
            ]%
            \addplot coordinates {(0,1) (1,0)};
            \addplot coordinates {(0,0.5) (1,0.5)};
            \legend{Graph 1, Graph 2};
\end{axis}%
%
\begin{axis}[
            axis x line                                 = none,
            axis y line*                                = right,
            legend style                                = {at={(0.5,-0.125)},
                anchor                                  = north,
                legend columns                          = 3,
                /tikz/every even column/.append style   = {column sep=0.5cm},
                /tikz/every odd column/.append style    = {column sep=0.15cm},
            },
            ]%
            \pgfplotsset%
                {%
                every outer y axis line/.style          = {xshift=2cm},
                every tick/.style                       = {xshift=2cm},
                every y tick label/.style               = {xshift=2cm},
                }%
            \addplot coordinates {(0.5,0) (0.5,1)};
            \legend{Graph 4};
\end{axis}%

\end{tikzpicture}

\end{document}

Screenshot of the result:

Screenshot of the result


Description of the issue:

As you can see, the legends are looking very ugly. How can I adjust the legends centered below the plot?


Update:

I've posted a further question about how to align the legend items in two rows or columns to let them appear nicely.

Dave
  • 3,758

1 Answers1

4

You could use legend to name for that.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{positioning}
\pgfplotsset{compat=newest, scale only axis}
\begin{document}

\begin{tikzpicture}
\begin{scope}[local bounding box=plots]
 \begin{axis}[
             axis y line*                                = left,
             legend to name                             = legA,
             legend style                                = {
                 legend columns                          = 3,
                 /tikz/every even column/.append style   = {column sep=0.5cm},
                 /tikz/every odd column/.append style    = {column sep=0.15cm},
                 },
             ]%
             \addplot coordinates {(0,0) (1,1)};
             \legend{Graph 1};
 \end{axis}%
 %
 \begin{axis}[
             axis x line                                 = none,
             axis y line*                                = right,
             legend to name                             = legB,
             legend style                                = {
                 legend columns                          = 3,
                 /tikz/every even column/.append style   = {column sep=0.5cm},
                 /tikz/every odd column/.append style    = {column sep=0.15cm},
             },
             ]%
             \addplot coordinates {(0,1) (1,0)};
             \addplot coordinates {(0,0.5) (1,0.5)};
             \legend{Graph 1, Graph 2};
 \end{axis}%
 %
 \begin{axis}[
             axis x line                                 = none,
             axis y line*                                = right,
             legend to name                             = legC,
             legend style                                = {
                 legend columns                          = 3,
                 /tikz/every even column/.append style   = {column sep=0.5cm},
                 /tikz/every odd column/.append style    = {column sep=0.15cm},
             },
             ]%
             \pgfplotsset%
                 {%
                 every outer y axis line/.style          = {xshift=2cm},
                 every tick/.style                       = {xshift=2cm},
                 every y tick label/.style               = {xshift=2cm},
                 }%
             \addplot coordinates {(0.5,0) (0.5,1)};
             \legend{Graph 4};
 \end{axis}%
\end{scope}
\node[below=0.5em of plots.south] (legB) {\ref{legB}};
\node[left=1em of legB] (legA) {\ref{legA}};
\node[right=1em of legB] (legC) {\ref{legC}};
\end{tikzpicture}

\end{document}

enter image description here

All legends in one box:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{positioning}
\pgfplotsset{compat=newest, scale only axis}
\begin{document}

\begin{tikzpicture}
\begin{scope}[local bounding box=plots]
 \begin{axis}[
             axis y line*                                = left,
             legend to name                             = legA,
             legend style                                = {draw=none,
                 legend columns                          = 3,
                 /tikz/every even column/.append style   = {column sep=0.5cm},
                 /tikz/every odd column/.append style    = {column sep=0.15cm},
                 },
             ]%
             \addplot coordinates {(0,0) (1,1)};
             \legend{Graph 1};
 \end{axis}%
 %
 \begin{axis}[
             axis x line                                 = none,
             axis y line*                                = right,
             legend to name                             = legB,
             legend style                                = {draw=none,
                 legend columns                          = 3,
                 /tikz/every even column/.append style   = {column sep=0.5cm},
                 /tikz/every odd column/.append style    = {column sep=0.15cm},
             },
             ]%
             \addplot coordinates {(0,1) (1,0)};
             \addplot coordinates {(0,0.5) (1,0.5)};
             \legend{Graph 1, Graph 2};
 \end{axis}%
 %
 \begin{axis}[
             axis x line                                 = none,
             axis y line*                                = right,
             legend to name                             = legC,
             legend style                                = {draw=none,
                 legend columns                          = 3,
                 /tikz/every even column/.append style   = {column sep=0.5cm},
                 /tikz/every odd column/.append style    = {column sep=0.15cm},
             },
             ]%
             \pgfplotsset%
                 {%
                 every outer y axis line/.style          = {xshift=2cm},
                 every tick/.style                       = {xshift=2cm},
                 every y tick label/.style               = {xshift=2cm},
                 }%
             \addplot coordinates {(0.5,0) (0.5,1)};
             \legend{Graph 4};
 \end{axis}%
\end{scope}
\node[below=0.5em of plots.south] (legB) {\ref{legB}};
\node[left=0.2em of legB] (legA) {\ref{legA}};
\node[right=0.2em of legB] (legC) {\ref{legC}};
\draw (legA.south west) rectangle (legC.north east);
\end{tikzpicture}
\end{document}

enter image description here

  • Thanks a lot! But would it be possible to add all legend items into one single legend? So that it looks "the classic way" with all items within one single border? – Dave Mar 19 '19 at 15:06
  • @Dave Yes. I added one way of doing that. (Of course, in the present plot you could just draw all plots in one axis and just build up one legend. I am assuming that your real life example has plots with different coordinate systems combined.) –  Mar 19 '19 at 15:11
  • Ah, thanks a lot for your great help! :-) I just posted a new screenshot to explain it better... Would it be possible to add the legends in two rows? Because right now the command legend columns = 3 does not work anymore. I think this command is not usable anymore with your solution, right? – Dave Mar 19 '19 at 15:13
  • Yes exactly, in the real coordinate system I have different axis scalings, e.g. 0 - 100 for the first axis and 0 - 20 for the second one etc. – Dave Mar 19 '19 at 15:14