2

I am looking for a way to draw arrows between each point in a sequence of points calculated in a foreach loop as intersections of vertical lines with a graph inspired from here.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest} 

\usepgfplotslibrary{fillbetween}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin=0,
            xmax=6,
            ymin=0,
            ymax=2]
            \addplot[
            name path global=A, 
            ]
            coordinates {
                (0,1)
                (6,1)
             };
            
            \foreach [evaluate=\i as \n using {5-5/2^(\i/2)}] \i in {1,...,20} {%
                \edef\temp{%
                    \noexpand \path[name path=AA-\i] (axis cs:\n,0) -- (axis cs:\n,2);
                    \noexpand \path[name intersections={of=A and AA-\i},];
                    \noexpand \coordinate (A1-\i)  at (intersection-1);
                    \noexpand \draw[fill=blue!50!white, opacity=0.5] (A1-\i) circle[radius=2pt] node (\i) {};
                }\temp
            }
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

For the nodes of the arrows I definded the nades as \i in my code and now I 'just' have to conect the 1st and 2nd, 2nd and 3rd, n-1th and nth node.

I already looked here and here, but this is not exactly what I am looking for

1

For a non-horizontal line, where it is easiest to take the points from the intersections rather than compute them directly, here's some code that works. I've simplified the original code slightly by defining nodes straight-away rather than coordinates and then drawing the circles. Then I've separated out the arrow-drawing code into a separate loop because I agree with the answer from user238301 that once the points get close together then the arrows don't look great so this makes it easy to select which nodes get connected.

\documentclass[border=5pt]{standalone}
%\url{https://tex.stackexchange.com/q/591212/86}
\usepackage{pgfplots}
\pgfplotsset{compat=newest} 
\usetikzlibrary{intersections,arrows.meta}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin=0,
            xmax=6,
            ymin=0,
            ymax=2]
            \addplot[
            name path global=A, 
            ]
            coordinates {
                (0,1)
                (6,1)
             };
            
            \foreach [evaluate=\i as \n using {5-5/2^(\i/2)}] \i in {1,...,20} {%
                \edef\temp{%
                    \noexpand \path[name path=AA-\i] (axis cs:\n,0) -- (axis cs:\n,2);
                    \noexpand \path[name intersections={of=A and AA-\i},] node[fill=blue!50!white,opacity=0.5,draw,inner sep=2pt,circle] (A1-\i)  at (intersection-1) {};
                }\temp
            }
            \foreach[evaluate=\i as \prev using int(\i-1)] \i in {2,...,5} {
            \edef\temp{\noexpand\draw[-Stealth] (A1-\prev) -- (A1-\i);}\temp
            }
        \end{axis}
    \end{tikzpicture}
\end{document}

arrows between nodes

1
  • Thanks for this general solution. I was not aware that the different intersections are somehow saved for futher processing.
    – Excelsior
    Apr 5 at 16:23
3

You do not need to let TikZ find the intersections between known horizontal and vertical lines, and the whole \edef\temp stuff is not needed here either. This draws these arrows, but as you can see as soon as the distance between the circle centers becomes smaller than twice the radius the result be comes a bit uncompelling.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest} 

\usepgfplotslibrary{fillbetween}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin=0,
            xmax=6,
            ymin=0,
            ymax=2]
            \addplot[
            name path global=A, 
            ]
            coordinates {
                (0,1)
                (6,1)
             };
            
            \path foreach [evaluate=\i as \n using {5-5/2^(\i/2)}] \i in {1,...,20} {%
             (\n,1) node[fill=blue!50!white, opacity=0.5,circle,inner
                sep=2pt,draw] (\i) {}
             \ifnum\i>1
              (\the\numexpr\i-1) edge[-stealth] (\i)
             \fi
             }; 
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

1
  • Thanks for your answer. Is your example also suitable to draw the arrows on a non-horizontal line?
    – Excelsior
    Apr 3 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.