[Tex/LaTex] Customize legend position in PGFplots axis environment

legendpgfplotspositioningtikz-pgf

I am trying to customize the position of the legend in the axis environment of PGFplots. A simple way to do this is by using something like

legend pos=south west|south east|north west|north east

in the axis options. If none of the directions suffice, we can use

legend style={at={(0.45,0.5)}}

, again in the axis options. It is, however, a bit annoying and inefficient to get the correct position by trial and error. Furthermore, it is unclear to me where

legend style={at={(0,0)}}

is. It would help me if I would know what coordinates the 'legend pos' command uses. Then I would already know the proper offset from the axis and I would only have to adjust one of the coordinates a bit.

Does anybody know how you can retrieve the coordinates that 'legend pos' uses for an axis environment?

Thanks in advance! And many thanks to Till Tantau and Christian Feuersanger for TikZ/PGF and PGFplots!

Best Answer

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} gives:

enter image description here

legend style={at={(1,1)},anchor=north east} gives:

enter image description here

EDIT

The pgfplots manual says:

enter image description here

The default position is north east. This means that north east correspond to a legend at a distance from axes of 2% the axes length. I imagine it is the same for the south west position, in that case it corresponds to:

legend style={at={(0.02,0.02)},anchor=south west}