
Como construir pontos por condição evitando a repetição de \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}
Responder1
Você pode usar 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}
que pode ser um pouco mais elegante com 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}
Alternativamente, você pode construir uma lista e plotá-la. Alguns cuidados devem ser tomados, pois quando o pgf itera nas listas, ...
ele introduz pequenos erros, por isso a condição é lida \ifdim\i pt<0.98pt
e não \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}