1

Occasionally we have legend entries which are of quite different sizes (lengths). This results in wasted real estate when legends are within the axis, and very large plots when they are outside the axis. If there are, for instance, roughly two lengths of legend entries (short and long) as shown below, it would be convenient to have a single legend box with customised dimensions.

My workaround for this has been to just create separate legend boxes:

\documentclass[convert]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[legend pos=south west, domain=0:100, no marks]
        \addplot+[orange] {0}; 
        \addlegendentry{long legend entry indeed}

        \addplot {-x + 1};  \label{a} 
        \addplot {-x + 4};  \label{b}
        \addplot {-x + 9};  \label{c}
        \addplot {-x + 1};  \label{d}

        % Desired look of legend box
        \draw (60,0) -- (80,0) -- (80, -20) -- (100,-20) -- (100, -30) -- (60, -30) -- (60, 0);
    \end{axis} % The following is quite tedious
    \begin{axis}[axis x line=none, axis y line=none, legend style={at={(axis cs:-5.63, -0.5)}, anchor=south west},]
        \addplot[opacity=0, forget plot] {0};
        \addlegendimage{/pgfplots/refstyle=a}  \addlegendentry{$c_1$}
        \addlegendimage{/pgfplots/refstyle=b}  \addlegendentry{$c_2$}
        \addlegendimage{/pgfplots/refstyle=c}  \addlegendentry{$c_3$}
        \addlegendimage{/pgfplots/refstyle=d}  \addlegendentry{$c_4$}
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Notice that if a single legend box was used, it would cover up a substantial region of the plot. This post looks relevant, although legend style options such as text depth operate globally.

algae
  • 285
  • IIRC, the legend is formed using a tabular. Alternatively, it is possible to construct a legent from scratch. See https://tex.stackexchange.com/questions/54794/using-a-pgfplots-style-legend-in-a-plain-old-tikzpicture – John Kormylo May 12 '20 at 14:04

1 Answers1

1

pgfplots does not seem to store the legend entries in nodes that we can easily access so the perhaps simplest option would be to use a path picture. Unfortunately, this means that we need to add two dimensions by hand.

\documentclass[convert]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[legend pos=south west,
        legend cell align=left,
        legend style={fill=none,draw=none,
        path picture={\draw 
        ([xshift=\pgflinewidth/2,yshift=-\pgflinewidth/2]path picture bounding box.north west)
         -- ++ (3.5em,0cm) |-
        ([xshift=-\pgflinewidth/2,yshift=1.8em]path picture bounding box.south east) --
        ([xshift=-\pgflinewidth/2,yshift=\pgflinewidth/2]path picture bounding box.south east) 
        -| cycle;}
        },
         domain=0:100, no marks]

        \addplot {-x + 1};  
        \addlegendentry{$c_1$} 
        \addplot {-x + 4}; 
        \addlegendentry{$c_2$}
        \addplot {-x + 9}; 
        \addlegendentry{$c_3$}
        \addplot {-x + 1};  
        \addlegendentry{$c_4$}
        \addplot+[orange] {0}; 
        \addlegendentry{long legend entry indeed}

        % Desired look of legend box
        \draw (60,0) -- (80,0) -- (80, -20) -- (100,-20) -- (100, -30) -- (60, -30) -- (60, 0);
    \end{axis} % The following is quite tedious

\end{tikzpicture}
\end{document}

enter image description here

If you remove all the path picture stuff and just keep fill=none you will get a legend without border, which my also do.