# How can I adjust the position of xlabel and ylabel with pgfplots?

I would like to have \sin x on top of the y-axis' arrow tip and x somewhere on the right of the x-axis' arrow tip. How can I achieve that? On top of that, I would like to know how I can tweak the distances myself.

There is 7.4.1 in the manual but using that code does not work out.

MWE (taken from p. 50)

\documentclass{scrartcl}

\usepackage{
pgfplots,
amsmath
}

\usepackage[T1]{fontenc}
\usepackage{lmodern}

%\pgfplotsset{every axis label/append .style={}}
%\pgfplotsset{every axis x label/append .style={
%at={(0.5,0)},
%right,
%yshift=5pt}}

\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
minor tick num=2,
axis y line=left,
axis x line=middle,
xlabel=$x$,ylabel=$\sin x$
]
plot[domain=-5:5,samples=40]
(\x,{sin(\x r)});
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}


pgfplots offers a couple of coordinate systems which are attached to either tick labels or only axes: at={(ticklabel cs:<position>)} or a starred variant ticklabel* cs.

The version without star shifts the label such that it is placed away from the axis, in a distance which matches the largest tick label. The version without star ignores the width of ticklabels, but chooses the axis on which tick labels are placed.

You could use ticklabel* cs here:

\documentclass{standalone}

\usepackage{
pgfplots,
amsmath
}

\usepackage[T1]{fontenc}
\usepackage{lmodern}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-2,
minor tick num=2,
axis y line=middle,
axis x line=middle,
xlabel=$x$,ylabel=$\sin x$,
every axis x label/.style={
at={(ticklabel* cs:1.05)},
anchor=west,
},
every axis y label/.style={
at={(ticklabel* cs:1.05)},
anchor=south,
},
]
plot[domain=-5:5,samples=40]
(\x,{sin(\x r)});
\end{axis}
\end{tikzpicture}
\end{document}


I took the freedom to change the displayed range and the axis placement to stress that this solution works even if the axis line is somewhere in the middle. It would even work for 3d axes, i.e. if the axis is rotated somehow.

The value ticklabel* cs:1.05 tells pgfplots to

• determine the correct axis in this context (context is "every axis x label/.style", i.e. we are referring to an x axis),
• go to 105% of that very axis were 0% is the lowest displayed limit and 100% is the largest displayed limit,
• place the node there.

The manual contains an indepth-discussion of these special coordinate systems and their variants (like those which include the axis name explicitly, i.e. xticklabel* cs).

• The explanation is very much appreciated, thanks! – henry Jul 8 '13 at 14:18

You can also work with the predefined node current axis:

\documentclass{scrartcl}

\usepackage{
pgfplots,
amsmath
}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
axis y line=left,
axis x line=middle,
every axis x label/.style={at={(current axis.right of origin)},anchor=west},
every axis y label/.style={at={(current axis.north west)},above=2mm},
minor tick num=2,
xlabel=$x$,ylabel=$\sin x$
]