我试图发布一个解决方案我怎样才能画出 25 条水平线来代替这 25 条标记的样本?,但我陷入了一个\foreach
循环,它的行为不符合我的预期。
这是我的第一次尝试:
\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}
我收到一个错误:
! Undefined control sequence.
<argument> axis cs:0,\myy
l.41 \end{axis}
?
我没想到会出现这样的错误。不过,我还是想出了一个笨拙的解决方法。这是我的第二次尝试,效果很好(虽然有点笨拙):
\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}
一切都很好,直到@Tarass 发帖一个答案这在概念上与我的非常相似,但避免了我尝试的笨重工作:
\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}
有人能解释一下这里发生了什么吗?特别是,为什么在环境\foreach
中调用循环时axis
健忘而需要从 进行处理after end axis
?
答案1
我猜问题是 TikZ/PGF 不喜欢
\draw (axis cs:0,\myy) -- (axis cs:360,\myy);
因为它想看到“明确的”坐标。使用通常的扩展技巧似乎有效:
\foreach \myn in {1,...,25}
{
\pgfmathsetmacro{\myx}{360/(25-1) * (\myn-1)}
\pgfmathsetmacro{\myy}{sin(\myx)}
\begingroup\edef\x{\endgroup
\noexpand\draw (axis cs:0,\myy) -- (axis cs:360,\myy);}%
\x
}
为什么与 Tarass 的代码不同?我无法解释。