pgfplots: Wie richte ich Legenden eines Multigraph-Diagramms aus?

pgfplots: Wie richte ich Legenden eines Multigraph-Diagramms aus?

Angenommen, wir haben das folgende MWE, um verschiedene Graphen in einem einzigen Diagramm anzuzeigen.


Minimales Arbeitsbeispiel (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 des Ergebnisses:

Screenshot des Ergebnisses


Beschreibung des Problems:

Wie Sie sehen, sehen die Legenden sehr hässlich aus. Wie kann ich die Legenden zentriert unter dem Plot anpassen?


Aktualisieren:

Ich habe geposteteine weitere FrageInformationen dazu, wie Sie die Legendenelemente in zwei Zeilen oder Spalten ausrichten, damit sie gut dargestellt werden.

Antwort1

legend to nameDafür könntest du verwenden .

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

Bildbeschreibung hier eingeben

Alle Legenden in einer 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}

Bildbeschreibung hier eingeben

verwandte Informationen