如何使用循环获取 y 轴上的等距离散点?我没有使用循环就完成了。请为此创建一个循环。
\documentclass[tikz, margin=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[%x=2cm,y=2cm
dot/.style = {circle, fill,
minimum size=.02ex,
}
]
\draw[-latex, thin, draw=gray] (-5,0)--(5,0) node [right] {$x$};
\draw[-latex, thin, draw=gray] (0,-5)--(0,5) node [above] {$y$};
%
\foreach \Point [count=\i] in {
(0,-5),(0,-4.5),(0,-4), (0,-3.5), (0,-3), (0,-2.5), (0,-2), (0,-1.5), (0,-1), (0,-.5), (0,0),
(0,5),(0,4.5),(0,4), (0,3.5), (0,3), (0,2.5), (0,2), (0,1.5), (0,1), (0,.5)
}
{
\node[dot] at \Point {};
}
\end{tikzpicture}
\end{document}
答案1
\documentclass[tikz, margin=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[%x=2cm,y=2cm
dot/.style = {circle, fill,
inner sep=.2ex,
}
]
\draw[-latex, thin, draw=gray] (-5,0)--(5,0) node [right] {$x$};
\draw[-latex, thin, draw=gray] (0,-5)--(0,5) node [above] {$y$};
%
\path foreach \Y in {-10,...,9}
{(0,\Y/2) coordinate[dot]};
\end{tikzpicture}
\end{document}
答案2
只需使用特殊的 foreach 语法:
\documentclass[tikz, margin=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
[dot/.style = {circle, fill, minimum size=.02ex, } ]
\draw[-latex, thin, draw=gray] (-5,0)--(5,0) node [right] {$x$};
\draw[-latex, thin, draw=gray] (0,-5)--(0,5) node [above] {$y$}; %
\foreach \y in {-5, -4.5,..., 5} { \node[dot] at (0,\y) {}; }
\end{tikzpicture}
\end{document}