1

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{
    compat=newest,
    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]
        \addplot[domain=-2:2, color=red, samples=50, smooth]{sin(pi*deg(x))};
        \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
1

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)},
}]
        \addplot[domain=-2:2, color=red, samples=50, smooth]{sin(pi*deg(x))};
        \addlegendentry{$\sin(x)$};
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

  • 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 What do you mean by standard spacing? – user11232 Oct 28 '15 at 14:11
  • @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
  • When using nodes in tikz, it is possible to position them according to another one, i.e. below. It does not make them touch one another. Here, the top right corner corresponds to the anchor. – Mammouth Oct 28 '15 at 14:45
  • @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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.