# PGFPlot : Relative positioning of legend according to axis

I am trying to move the legend of a PGFPlot above the top right corner, but using relative positioning like at={(current axis.above of north east)} in stead of manually giving a position like at={(1, 1.02)}. Would it be possible ?

In the Tikz documentation, there is something like legend=north outside but such a syntax seems not to exist within PGFPlots.

Here is a minimal working example :

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}

\pgfplotsset{
every axis legend/.append style = {
anchor=south east,
at={(1,1.02)},
draw=none
}
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[grid=major, enlarge x limits=false]
\addlegendentry{$\sin(x)$};
\end{axis}
\end{tikzpicture}
\end{document}

• The only predefined outside position seems to be legend pos=outer north east. Look up legend pos in the manual. – Torbjørn T. Oct 28 '15 at 11:41
• @TorbjørnT. At page 228 of PGFPlots manual (version 1.12.1), in the chapter corresponding to colorbar right, there is the use of at={(parent axis.right of north east)}. It is not possible to apply that to other objects ? – Mammouth Oct 28 '15 at 12:47
• parent axis seems to apply only for colorbars, don't know if anything like that can be applied to the legend. – Torbjørn T. Oct 28 '15 at 12:57

Try

 anchor=south east,
at= {(current axis.north east)}


Full code:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}

\pgfplotsset{
compat=1.12,
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[grid=major, enlarge x limits=false,
legend style={
anchor=south east,
%cells={anchor=east},
draw=none,
at= {(current axis.north east)},
}]
\addlegendentry{$\sin(x)$};

• This is also what I tried, but it does not add a "standard" spacing between the plot axis and its legend. But I understand the answer that no, the syntax does not exist without using some additional commands. Would it be possible to transfer the problem to the anchor ? My unsuccessful try is the following : anchor={below of south east}. I think my confusion comes form the fact that I don't know precisely the different behaviors of a regular node and the legend. – Mammouth Oct 28 '15 at 12:33
• @Mammouth: anchor=south east, at= {(current axis.north east)} is equivalent of current axis.above of north east. – user11232 Oct 28 '15 at 14:13
• @Mammouth: If you mean the vertical separation, it can be adjusted by inner ysep. at is different from above – user11232 Oct 28 '15 at 15:00