
繰り返しを避けながら条件によってポイントを構築する方法 \addplot
。
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\pgfplotsinvokeforeach{0.3,0.4,...,1} {
\addplot coordinates { #1 < 1 ? (#1,#1) : (5,5) }; % if \i < 1 then put (x,y) on axis, else put (5,5) point on axis
}
\end{axis}
\end{tikzpicture}
\end{document}
答え1
を使用できますsamples at
。
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[blue,mark=*,samples at={0.3,0.4,...,1}]
({ifthenelse(x < 1,x,5)},{ifthenelse(x < 1,x,5)}); % if \i < 1 then put (x,y) on axis, else put (5,5) point on axis
\end{axis}
\end{tikzpicture}
\end{document}
これをもう少しエレガントにするにはdeclare function
、
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[declare function={f(\x)=ifthenelse(\x<1,\x,5);}]
\begin{axis}
\addplot[blue,mark=*,samples at={0.3,0.4,...,1}]
({f(x)},{f(x)}); % if \i < 1 then put (x,y) on axis, else put (5,5) point on axis
\end{axis}
\end{tikzpicture}
\end{document}
あるいは、リストを構築してプロットすることもできます。pgf が のリストを反復処理するときに...
小さなエラーが発生するため、条件が\ifdim\i pt<0.98pt
ではなく と表示されるため、注意が必要です\ifdim\i pt<1pt
。
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\edef\mylst{}
\pgfplotsforeachungrouped \i in {0.3,0.4,...,1} {%
\ifdim\i pt<0.98pt
\edef\mylst{\mylst (\i,\i)}
\else
\edef\mylst{\mylst (5,5)}
\fi
}%
\addplot coordinates {\mylst};
\end{axis}
\end{tikzpicture}
\end{document}