2

The user marmot has posted a nice approach on how to adjust the plot width to the legend width.


Minimum Working Example (MWE):

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

\begin{document}
\pgfplotsset{superb legend/.style={legend style          = {draw=none,
                 legend columns                          = 3,
                 /tikz/every even column/.append style   = {column sep=0.5cm,
                 text width=7em},
                 /tikz/every odd column/.append style    = {column sep=0.15cm,
                  text width=7em},
                 }}}

\begin{tikzpicture}
\ifdefined\mylegendwidth
\ifdim\mylegendwidth<0pt
\pgfmathsetmacro{\mywidth}{abs(\mylegendwidth)}
\def\mylegendwidth{\mywidth pt}
\fi
\else
\typeout{Please\space recompile\space your\space file!}
\def\mylegendwidth{10cm}
\fi
\pgfplotsset{width=\mylegendwidth}

\begin{scope}[local bounding box=plots]
 \begin{axis}[
             axis y line*                                = left,
             legend to name                              = legA,
             superb legend
             ]%
             \addplot coordinates {(0,0) (1,1)};
             \addplot coordinates {(1,0) (0,1)};
             \addplot coordinates {(0,1) (1,1)};
             \legend{Graph 1, Graph 2, Graph 3};
 \end{axis}%
 %
 \begin{axis}[
             axis x line                                 = none,
             axis y line*                                = right,
             legend to name                              = legB,
             superb legend
             ]%
             \addplot coordinates {(0,1) (1,0)};
             \addplot coordinates {(0,0.5) (1,0.5)};
             \addplot coordinates {(0,0.15) (1,0.15)};
             \legend{Graphinggraph~4, Graphinggraph~5, Graphinggraph~6};
 \end{axis}%
 %
 \begin{axis}[
             axis x line                                 = none,
             axis y line*                                = right,
             legend to name                              = legC,
             superb legend
             ]%
             \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)};
             \addplot coordinates {(0.6,0) (0.6,1)};
             \addplot coordinates {(0.7,0) (0.7,1)};
             \legend{Graphgraph 7, Graphgraph 8, Graphgraph 9};
 \path (current axis.west) coordinate (aux1)  ([xshift=2cm]current axis.east)
  coordinate (aux2);
 \end{axis}%
\end{scope}

\node[anchor=north west] at ([yshift=-3.5em]plots.south-|aux1) (legA) {\ref{legA}};
\node[below=-0.5em of legA] (legB) {\ref{legB}};
\node[below=-0.5em of legB] (legC) {\ref{legC}};
\draw let \p1=($(legC.east)-(legA.west)$),\p2=($(aux2)-(aux1)$) in
[/utils/exec=\pgfmathsetmacro{\mywidth}{\mylegendwidth+\x1-\x2}%
\xdef\mylegendwidth{\mywidth pt}]
 (legA.north west) rectangle (legC.south east);

\makeatletter
 \immediate\write\@mainaux{\xdef\string\mylegendwidth{\mylegendwidth}\relax}
\makeatother

\end{tikzpicture}
\end{document}

Screenshot of the result:

Screenshot of the result


Unfortunately when displaying huge amounts of data points, LaTeX will complain about exceeded memory. To avoid this behavior Joseph Wright has posted an approach on how to externalize compiling the plots:

Mr. Joseph Wright wrote:

  • Adding

    \usepgfplotslibrary{external} 
    \tikzexternalize
    

    to the preamble

  • and typing

    pdflatex -shell-escape <filename>
    

    into the shell should work.


However, after extending the code with this and recompiling, the legend won't match with the plot anymore:

Screenshot of the issue


Is there an option available to avoid this behavior and get the plot matching again?


Minimum Working Example (MWE) including the edits based on Joseph Wright:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{positioning,calc}
% -------------------------------------------------
% This is new:
\usepgfplotslibrary{dateplot, statistics, external}
\tikzexternalize
% -------------------------------------------------
\pgfplotsset{compat=newest, scale only axis}
\begin{document}
\pgfplotsset{superb legend/.style={legend style          = {draw=none,
                 legend columns                          = 3,
                 /tikz/every even column/.append style   = {column sep=0.5cm,
                 text width=7em},
                 /tikz/every odd column/.append style    = {column sep=0.15cm,
                  text width=7em},
                 }}}
\begin{tikzpicture}
\ifdefined\mylegendwidth
\ifdim\mylegendwidth<0pt
\pgfmathsetmacro{\mywidth}{abs(\mylegendwidth)}
\def\mylegendwidth{\mywidth pt}
\fi
\else
\typeout{Please\space recompile\space your\space file!}
\def\mylegendwidth{10cm}
\fi
\pgfplotsset{width=\mylegendwidth}
\begin{scope}[local bounding box=plots]
 \begin{axis}[
             axis y line*                                = left,
             legend to name                              = legA,
             superb legend
             ]%
             \addplot coordinates {(0,0) (1,1)};
             \addplot coordinates {(1,0) (0,1)};
             \addplot coordinates {(0,1) (1,1)};
             \legend{Graph 1, Graph 2, Graph 3};
 \end{axis}%
 %
 \begin{axis}[
             axis x line                                 = none,
             axis y line*                                = right,
             legend to name                              = legB,
             superb legend
             ]%
             \addplot coordinates {(0,1) (1,0)};
             \addplot coordinates {(0,0.5) (1,0.5)};
             \addplot coordinates {(0,0.15) (1,0.15)};
             \legend{Graphinggraph~4, Graphinggraph~5, Graphinggraph~6};
 \end{axis}%
 %
 \begin{axis}[
             axis x line                                 = none,
             axis y line*                                = right,
             legend to name                              = legC,
             superb legend
             ]%
             \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)};
             \addplot coordinates {(0.6,0) (0.6,1)};
             \addplot coordinates {(0.7,0) (0.7,1)};
             \legend{Graphgraph 7, Graphgraph 8, Graphgraph 9};
 \path (current axis.west) coordinate (aux1)  ([xshift=2cm]current axis.east)
  coordinate (aux2);
 \end{axis}%
\end{scope}
\node[anchor=north west] at ([yshift=-3.5em]plots.south-|aux1) (legA) {\ref{legA}};
\node[below=-0.5em of legA] (legB) {\ref{legB}};
\node[below=-0.5em of legB] (legC) {\ref{legC}};
\draw let \p1=($(legC.east)-(legA.west)$),\p2=($(aux2)-(aux1)$) in
[/utils/exec=\pgfmathsetmacro{\mywidth}{\mylegendwidth+\x1-\x2}%
\xdef\mylegendwidth{\mywidth pt}]
 (legA.north west) rectangle (legC.south east);
\makeatletter
 \immediate\write\@mainaux{\xdef\string\mylegendwidth{\mylegendwidth}\relax}
\makeatother
\end{tikzpicture}
\end{document}

Your Answer

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

Browse other questions tagged or ask your own question.