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$
]
\addplot[blue,mark=none]
plot[domain=-5:5,samples=40]
(\x,{sin(\x r)});
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
Best Answer
pgfplots offers a couple of coordinate systems which are attached to either tick labels or only axes:
at={(ticklabel cs:<position>)}
or a starred variantticklabel* 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: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 toevery axis x label/.style
", i.e. we are referring to an x axis),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
).