I was trying to post a solution to How can I draw 25 horizontal lines instead of these 25 marked samples?, but I got stuck with a \foreach loop which wasn't behaving as I expected it to.
Here's my first attempt:
\documentclass[a4paper]{scrartcl}
\usepackage{tikz,pgfplots,amsmath}
\usepackage[T1]{fontenc}
\usepackage{lmodern,textcomp,siunitx}
\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]
\begin{axis}
[
height=5cm,
width=6cm,
scale only axis=true,
%
xlabel={\(\alpha\) in \si{\degree}},
ylabel={\(\sin{(\alpha)}\)},
%
xtick=\empty,
ytick=\empty,
xticklabels={,,},
yticklabels={,,},
xmajorgrids={false},
ymajorgrids={false}
]
\addplot+[domain=0:360,
samples=25,
only marks,
mark=-]
{sin(x)};
\foreach \myn in {1,...,25}
{
\pgfmathsetmacro{\myx}{360/(25-1) * (\myn-1)}
\pgfmathsetmacro{\myy}{sin(\myx)}
\draw (axis cs:0,\myy) -- (axis cs:360,\myy);
}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
I get an error:
! Undefined control sequence.
<argument> axis cs:0,\myy
l.41 \end{axis}
?
I wasn't expecting such an error. Nevertheless, I came up with a clunky work around. Here's my 2nd attempt which works quite well (though a bit clunky):
\documentclass[a4paper]{scrartcl}
\usepackage{tikz,pgfplots,amsmath}
\usepackage[T1]{fontenc}
\usepackage{lmodern,textcomp,siunitx}
\makeatletter
\def\aedrawline#1{%%
\edef\mycontent{#1}
\expandafter\ae@drawline\mycontent\@nil}
\def\ae@drawline#1,#2,#3\@nil{%%
\draw (axis cs:#1,#3) -- (axis cs:#2,#3);
}
\makeatother
\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]
\begin{axis}
[
height=5cm,
width=6cm,
scale only axis=true,
%
xlabel={\(\alpha\) in \si{\degree}},
ylabel={\(\sin{(\alpha)}\)},
%
xtick=\empty,
ytick=\empty,
xticklabels={,,},
yticklabels={,,},
xmajorgrids={false},
ymajorgrids={false}
]
\addplot+[domain=0:360,
samples=25,
only marks,
mark=-]
{sin(x)};
\foreach \myn in {1,...,25}
{
\pgfmathsetmacro{\myx}{360/(25-1) * (\myn-1)}
\pgfmathsetmacro{\myy}{sin(\myx)}
\aedrawline{0,
360,
\myy}
}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
All's good until @Tarass posts an answer which is very similar in concept to mine but avoids the clunky work around of my attempts:
\documentclass[a4paper]{scrartcl}
\usepackage{tikz,pgfplots,amsmath}
\usepackage[T1]{fontenc}
\usepackage{lmodern,textcomp,siunitx}
\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]
\begin{axis}
[
height=5cm,
width=6cm,
scale only axis=true,
%
xlabel={\(\alpha\) in \si{\degree}},
ylabel={\(\sin{(\alpha)}\)},
%
xtick=\empty,
ytick=\empty,
xticklabels={,,},
yticklabels={,,},
xmajorgrids={false},
ymajorgrids={false},
after end axis/.code={%%
\foreach \myn in {1,...,25}
{
\pgfmathsetmacro{\myx}{360/(25-1) * (\myn-1)}
\pgfmathsetmacro{\myy}{sin(\myx)}
\draw (axis cs:0,\myy) -- (axis cs:360,\myy);
}
}
]
\addplot+[domain=0:360,
samples=25,
only marks,
mark=-]
{sin(x)};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
Could someone explain what's going on here? In particular, why it that when the \foreach loop is called within the axis environment it is forgetful and needs instead to be processed from after end axis?

sin(\i). In your code, you have\myyand\myxso you have to expand. You could probably use\pgfplotsinvokeforeachwith Tarass. – dustin Apr 27 '14 at 16:19