我想绘制一个采样正弦波。MWE 如下。错误是\hoek
和\sinus
未定义。
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[]
\addplot [domain=0:360,mark=none,samples=201,red]{0.5*sin(x)+0.5};
\foreach \count in {0.0,1.0,...,16.0}{%
\pgfmathsetmacro{\hoek}{\count*360.0/16.0};
\pgfmathsetmacro{\sinus}{0.5*sin(\count*360.0/16.0)+0.5};
\draw (\hoek,\sinus) circle[radius=2pt];
}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
\foreach
循环在图表中不能很好地工作pgfplots
。- 可以使用
ycomb
选项简单地绘制样本:
\documentclass[12pt]{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot [red, domain=0:360,mark=none,samples=201] {0.5*sin(x)+0.5};
\addplot [ycomb, red, domain=0:360,
mark=*, samples=18] {0.5*sin(x)+0.5};
\end{axis}
\end{tikzpicture}
\end{document}
编辑: 更精美一点的图表,为了有趣和锻炼......
\documentclass[12pt, margin=3mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
x = 0.8,
xtick={0,30,...,360},
extra y ticks={0, 0.5},
extra y tick style={grid=major, dashed},
ytick={0,0.25,...,1},
ticklabel style={font=\footnotesize},
domain=0:360,
]
\addplot [red, thick, mark=none, samples=201] {0.5*sin(x)+0.5};
\addplot [ycomb, red, samples=13,
mark=*, mark options={scale=0.75, fill=white}] {0.5*sin(x)+0.5};
\end{axis}
\end{tikzpicture}
\end{document}