[Tex/LaTex] PGFPLOTS transposed legend with empty legend entries

legendpgfplots

I am having problems to correctly place the legend entries in a 3×3 shaped, transposed legend matrix with empty (skipped) entries for the following MWE:

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

  \begin{tikzpicture}
    \begin{axis}[%
      legend columns = 3,
      transpose legend,
    ]

    \addplot {-x+1};\addlegendentry{A}
    \addplot {-x+2};\addlegendentry{B}
    \addplot {-x+3};\addlegendentry{C}
    \addplot {-x+4};\addlegendentry{D}
    \addplot {-x+5};\addlegendentry{E}
    \addlegendimage{empty legend}\addlegendentry{skip me}
    \addplot {-x+7};\addlegendentry{F}
    %\addlegendimage{empty legend}\addlegendentry{skip me}
    %\addlegendimage{empty legend}\addlegendentry{skip me}

    \end{axis}
  \end{tikzpicture}
\end{document}

What I get is:

failing example

But what I expect is:

A   D   F
B   E
C

I figured out that I get the expected result by uncommenting the last two empty legend-lines:

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

  \begin{tikzpicture}
    \begin{axis}[%
      legend columns = 3,
      transpose legend,
    ]

    \addplot {-x+1};\addlegendentry{A}
    \addplot {-x+2};\addlegendentry{B}
    \addplot {-x+3};\addlegendentry{C}
    \addplot {-x+4};\addlegendentry{D}
    \addplot {-x+5};\addlegendentry{E}
    \addlegendimage{empty legend}\addlegendentry{skip me}
    \addplot {-x+7};\addlegendentry{F}
    \addlegendimage{empty legend}\addlegendentry{skip me} % <-- Uncommented
    \addlegendimage{empty legend}\addlegendentry{skip me} % <-- Uncommented

    \end{axis}
  \end{tikzpicture}
\end{document}

With this, I get the expected result:

working example

Although I found the solution for my initial problem, I am wondering if this is the expected behavior!? I couldn't find anything related in the PGFPLOTS documentation.

Best Answer

If this meets your requirement please tick the check-mark on the left

enter image description here

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

  \begin{tikzpicture}
    \begin{axis}[%
      legend columns = 3,
      transpose legend,
    ]

    \addplot {-x+1};\addlegendentry{A}
    \addplot {-x+2};\addlegendentry{B}
    \addplot {-x+3};\addlegendentry{C}
    \addplot {-x+4};\addlegendentry{D}
    \addplot {-x+5};\addlegendentry{E}
    \addlegendimage{empty legend}\addlegendentry{}
    \addplot {-x+7};\addlegendentry{F}
    \addlegendimage{empty legend}\addlegendentry{} % <-- Uncommented
    \addlegendimage{empty legend}\addlegendentry{} % <-- Uncommented

    \end{axis}
  \end{tikzpicture}
\end{document}

Do keep in mind you have also to specify an anchor of your legend in this way:

legend style={at={(0.03,0.5)},anchor=west}

the anchor define what point of the legend box will be placed at the coordinates you define with at={(<>,<>)}.

If you use only at={(<>,<>)} the coordinates you insert are that of the axis box where the point (0,0) is the left bottom angle and (1,1) the right top angle.

If you use instead at={(axis cs:<>,<>)} you specify the real coordinates of the axis, the same of your plot.

EXAMPLES

legend style={at={(axis cs:0.5,1)},anchor=south west}

or

\begin{axis}[legend pos=north west]

or

\begin{axis}[legend style={at={(0.5,-0.1)},anchor=north}]

or

\begin{axis}
[%
    axis lines = middle,
    xlabel = $x$,
    ylabel = {$f(x)$},
    enlarge y limits=upper,
    yticklabel style = {font=\tiny},
    xticklabel style = {font=\tiny},
    legend style={xshift=1.5cm},
    thick,
]%

The default position is north east.