我有以下代码来生成一个简单的间隔
\begin{tikzpicture}[scale=3, nodes={
execute at begin node=$,
execute at end node=$
}]
\draw[-, thick] (-1,0) -- (1,0) node[above] {};
\foreach \x/\xpar/\xtext in {
-0.8 / | / +,
-0.6 / | / ,
-0.4 / | / +,
-0.2 / | / -,
0 / | / ,
.2 / | / -,
.4 / | / +,
.6 / | / -,
.8 / | / +,
} \draw[thick] (\x,0pt) node {\xpar} node[below=5pt] {\xtext};
\end{tikzpicture}
产生
有没有办法让我在这个区间绘制 sin 函数,以便得到类似
答案1
\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[scale=3, nodes={
execute at begin node=$,
execute at end node=$
}]
\draw[-, thick] (-1,0) -- (1,0) node[above] {};
\foreach \x/\xpar/\xtext in {
-0.8 / | / +,
-0.6 / | / ,
-0.4 / | / +,
-0.2 / | / -,
0 / | / ,
.2 / | / -,
.4 / | / +,
.6 / | / -,
.8 / | / +,
} \draw[thick] (\x,0pt) node {\xpar} node[below=5pt] {\xtext};
\draw[blue] plot[variable=\x,samples=100,domain=-1:1] (\x,{0.2*sin((\x-0.3)*900)});
\end{tikzpicture}
\end{document}
一个更简单也许更美好的可能性:
\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[scale=3, nodes={
execute at begin node=$,
execute at end node=$
}]
\draw[-, thick] (-1,0) -- (1,0) node[above] {};
\foreach \x/\xpar/\xtext in {-0.8,-0.6,...,0.8}
{\pgfmathtruncatemacro{\y}{sign(sin((\x-0.3)*900))}
\ifnum\y=1
\def\xtext{+}
\else
\def\xtext{-}
\fi
\draw (\x,-2pt) -- (\x,2pt);
\node at (\x,{-\y*5pt}) {\xtext};}
\draw[blue] plot[variable=\x,samples=100,domain=-1:1] (\x,{0.2*sin((\x-0.3)*900)});
\end{tikzpicture}
\end{document}