13

I have a pgfplot where the legend is right to the plot. I create the plot as follows:

\begin{axis}[
    scale only axis,
    xmode=log,
    ymode=log,
    width=7cm,
    legend cell align=left,
    legend pos=outer north east,
    legend style={draw=none}
  ]

Now I would like to move the legend down below the plot. So I thought I have just to replace legend pos=outer north east by legend pos=outer south west. But LaTex does not like this idea: Choice 'outer south west' unknown in key '/pgfplots/le gend pos'. Whats wrong with this?

Thomas W.
  • 703

1 Answers1

15

From section 4.8.5 of the manual, you can use every axis legend:

\pgfplotsset{every axis legend/.append style={
at={(0,0)},
anchor=north east}} 

Code: (partially from manual )

\documentclass{standalone}
\usepackage{pgfplots}
 \pgfplotsset{width=7cm,compat=1.7}
 \pgfplotsset{every axis legend/.append style={
    at={(0,0)},
    anchor=north east}}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot coordinates {(0,0) (1,1)};
\addplot coordinates {(0,1) (1,2)};
\addplot coordinates {(0,2) (1,3)};
\legend{$l_1$,$l_2$,$l_3$}
\end{axis}
\end{tikzpicture}
\end{document}

Instead of the /.append style, it is possible to use legend style as in the following example. It has the same effect.

\documentclass{standalone}
\usepackage{pgfplots}
 \pgfplotsset{width=7cm,compat=1.7}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend style={
at={(0,0)},
anchor=north east}]
\addplot coordinates {(0,0) (1,1)};
\addplot coordinates {(0,1) (1,2)};
\addplot coordinates {(0,2) (1,3)};
\legend{$l_1$,$l_2$,$l_3$}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

As percusse mentioned in comment, you can fine tune the position by at={(axis description cs:0,-0.1)} as in:

\documentclass{standalone}
\usepackage{pgfplots}
 \pgfplotsset{width=7cm,compat=1.7}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend style={
at={(0,0)},
anchor=north east,at={(axis description cs:0,-0.1)}}]
\addplot coordinates {(0,0) (1,1)};
\addplot coordinates {(0,1) (1,2)};
\addplot coordinates {(0,2) (1,3)};
\legend{$l_1$,$l_2$,$l_3$}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • When I try your solution and put anchor=north west, its exactly what I want but the x-axis labels are hidden by the legend. Any proposal how to circumvent this problem? – Thomas W. Nov 19 '12 at 13:12
  • @ThomasWitkowski Use anchor=north west,at={( xticklabel cs:1)} or anchor=north west,at={(axis description cs:1.05,-0.05)} – percusse Nov 19 '12 at 13:15
  • 1
    If anyone else checks this, one can center the legend box below the plot via anchor=north,at={(axis description cs:0.5,-0.18)},. The -0.18 should suffice for the y-offset due to the xlabel, at least it does in my case. I'd also recommend to use multiple legend columns with legend columns={..insert number here} then. – henry Jun 24 '14 at 14:01
  • @henry: Thanks so much for your comment, that was exactly what I came looking for. For the record: An effective way to get the same effect without manually adjusting is to use legend to name and referring to it directly after the picture. – Paul Paulsen Jan 30 '15 at 13:06