2

I'm trying to draw arrowed lines that goes like (A) -> (A1) -> (A2) -> (A3), but my code doesn't work.

\documentclass[11pt,a4paper]{article}
\usepackage{geometry}
\usepackage{tikz}
\usepackage{pgfplots, relsize}
\usepgfplotslibrary{polar}
\usepgflibrary{shapes.geometric}
\usetikzlibrary{calc}

\usetikzlibrary{arrows,decorations.markings}
\begin{document}

\begin{center}
\begin{tikzpicture}
  \begin{axis}[
  unit vector ratio*=1 1 1,
   grid = none,
    axis x line = middle,
    axis y line = middle,
    xlabel={$k_t$},
    xlabel style={at=(current axis.right of origin), anchor=west},
    ylabel={$k_{t+1}$},
    ylabel style={at=(current axis.above origin), anchor=south},
    xmin = 0,
    xmax = 15,
    scale=1.5,
    enlarge y limits={rel=0.13},
    enlarge x limits={rel=0.07},
    ymin = 0,
    ymax = 10,  xtick=\empty,ytick=\empty,
    after end axis/.code={\path (axis cs:0,0) node [anchor=north west] {0};},
  ]
    \coordinate (0,0) ;
    % domain can be set individually!!
    \addplot[color=red, domain=0:10,samples=100,smooth,thick] {x} node[above,pos=1] {$45^\circ$};
    \addplot[color=blue,domain=0:11,samples=1000,smooth, thick] {3*sqrt(x)} node[right,pos=1] {$W(s,k_{t},\delta)$};
    \addplot[color=Cerulean,domain=0:11,samples=1000,smooth, thick] {2*sqrt(x)} node[right,pos=1] {$W(s',k_{t},\delta)$};

    \addplot[mark=*,only marks, fill=white] coordinates {(9,9)} node[above, pos=1]{$A$};
    \addplot[mark=*,only marks, fill=white] coordinates {(4,4)} node[above, pos=1]{$A'$};

    \addplot[mark=*,only marks, fill=white] coordinates {(9,6)} node[below, pos=1]{$A_1$};
    \addplot[mark=*,only marks, fill=white] coordinates {(6,6)} node[above, pos=1]{$A_2$};
    \addplot[mark=*,only marks, fill=white] coordinates {(6,4.899)} node[below, pos=1]{$A_3$};
    \addplot[mark=*,only marks, fill=white] coordinates {(4.899,4.899)} node[above, pos=1]{$A_4$};

    % NO output shown at all
    \node (A) at (9, 9){};
    \node (Ap) at (4, 4){};
    \node (A1) at (9, 6){};
    \node (A2) at (6, 6){};
    \node (A3) at (6, 4.899){};
    \node (A4) at (4.899, 4.899){};
    \draw [dashed] (A) -- (A1) -- (A2) -- (A3) -- (A4);
  \end{axis}

\end{tikzpicture}
\end{center}
\end{document}

enter image description here

The output is seriously out of proportion (it's the tiny dashes near the origin in case you couldn't find it). The position and scale are way off. Interestingly, when I put the \node declarations and \draw statement outside {axis}, the output is again way off, but is off in the opposite direction. I believe I haven't defined the \node objects properly with regard to the {axis} coordinates and they all got positioned in some other coordinate frame.

Any idea on how to solve this issue? Thanks!

Vim
  • 197
  • 2
    You need to either att \pgfplotsset{compat=1.11} (or a higher value) in the preamble or prepend the TikZ coordinates by axis cs: otherwise the tikzpicture coordinates and not the axis coordinates are used. – Stefan Pinnow Dec 17 '18 at 17:58
  • @StefanPinnow by the way, are you familiar with how to draw consecutive arrows in an elegant way? With \draw[->], only the last segment has arrow tip. Thanks! – Vim Dec 17 '18 at 18:08
  • You can use e.g. edges for that: \draw [dashed,-latex] (A) edge (A1) (A1) edge (A2) (A2) edge (A3) (A3) edge (A4);. Or you can use decorations. –  Dec 17 '18 at 18:09

3 Answers3

2

As already mentioned in the comment below the question the nodes are "displaced" because you have (accidentally) used the wrong coordinate system for your TikZ coordinates. To circumvent this can either state \pgfplotsset{compat=1.11} (or a higher value) in the preamble or prepend the TikZ coordinates by axis cs: otherwise the tikzpicture coordinates and not the axis coordinates are used.

Here I present a "cleaned" attempt to achieve what you want without the need of repetitions as you had to and this also works without the need of the aforementioned stuff. As marmot already stated in his comment below the question you can make use of edge to draw the connecting line with the arrows, but I borrowed the decoration way from his answer which is much nicer.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usetikzlibrary{decorations.pathreplacing}
    \pgfplotsset{
        % declare the style of the marks/nodes here (once)
        points/.style={
            mark=*,
            only marks,
            fill=white,
        },
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis lines=middle,
        xlabel={$k_t$},
        xlabel style={
            at=(current axis.right of origin),
            anchor=west,
        },
        ylabel={$k_{t+1}$},
        ylabel style={
            at=(current axis.above origin),
            anchor=south,
        },
        xmin=0,
        xmax=15,
        scale=1.5,
        enlarge y limits={rel=0.13},
        enlarge x limits={rel=0.07},
        ymin=0,
        ymax=10,
        xtick=\empty,
        ytick=\empty,
        after end axis/.code={
            \path (0,0) node [anchor=north west] {0};
        },
        % ---------------------------------------------------------------------
        % moved common `\addplot' options here
        smooth,
        % there is no need to use that many coordinates
        % (to further decrease them you can adapt use "non-linear spacing"
        %  between the points as e.g. shown at
        %  <https://tex.stackexchange.com/a/373820/95441>)
        samples=201,
        domain=0:11,
    ]
        \addplot [red,thick,domain=0:10,samples=2] {x}
            node [above] {$45^\circ$}
        ;
        \addplot [blue,thick] {3*sqrt(x)}
            node [right] {$W(s,k_{t},\delta)$}
        ;
        \addplot [green,thick] {2*sqrt(x)}
            node [right] {$W(s',k_{t},\delta)$}
        ;

        \addplot [
            points,
            % specify the points where the points should be shown ...
            samples at={4,4.899,6,9},
        ] {x}
            % ... and add nodes with labels accordingly
            node [pos=0.00,label=$A'$]  (Ap) {}
            node [pos=0.25,label=$A_4$] (A4) {}
            node [pos=0.50,label=$A_2$] (A2) {}
            node [pos=1.00,label=$A$]   (A)  {}
        ;
        \addplot [
            points,
            samples at={6,9},
        ] {2*sqrt(x)}
            node [pos=0,label=below:$A_3$] (A3) {}
            node [pos=1,label=below:$A_1$] (A1) {}
        ;

        % draw the connecting line between the points
        \draw [
            decorate,
            decoration={
                show path construction,
                lineto code={
                    \draw [dashed,-latex]
                        (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast)
                    ;
                }
            },
        ] (A) -- (A1) -- (A2) -- (A3) -- (A4);
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535
1

You have to use the axis cs coordinate system for specifying points inside an axis in pgfplots. There are several other coordinate systems that can be used (refer to the manual for those) but in your case that is the one you need.
Your code therefore becomes (I left out the unchanged parts)

\node (A) at (axis cs:9, 9){};
\node (Ap) at (axis cs:4, 4){};
\node (A1) at (axis cs:9, 6){};
\node (A2) at (axis cs:6, 6){};
\node (A3) at (axis cs:6, 4.899){};
\node (A4) at (axis cs:4.899, 4.899){};
\draw [dashed] (A) -- (A1) -- (A2) -- (A3) -- (A4);

I order to change the used coordinate system one has to prefix the first coordinate specification with the respective name and a colon as I have done in the given example.

Raven
  • 3,023
1

This is an answer only on the multiple arrows part. The fact that one should use a newer version of pgfplots compatibility has already been pointed out by Stefan Pinnow. Here I show how to draw the multiple arrow heads by (ab)using show path construction.

\documentclass[11pt,a4paper]{article}
\usepackage{geometry}
\usepackage{tikz}
\usepackage{pgfplots, relsize}
\usepgfplotslibrary{polar}
\usepgflibrary{shapes.geometric}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.16}
\usetikzlibrary{arrows,decorations.markings,decorations.pathreplacing}
\begin{document}

\begin{center}
\begin{tikzpicture}
  \begin{axis}[
  unit vector ratio*=1 1 1,
   grid = none,
    axis x line = middle,
    axis y line = middle,
    xlabel={$k_t$},
    xlabel style={at=(current axis.right of origin), anchor=west},
    ylabel={$k_{t+1}$},
    ylabel style={at=(current axis.above origin), anchor=south},
    xmin = 0,
    xmax = 15,
    scale=1.5,
    enlarge y limits={rel=0.13},
    enlarge x limits={rel=0.07},
    ymin = 0,
    ymax = 10,  xtick=\empty,ytick=\empty,
    after end axis/.code={\path (axis cs:0,0) node [anchor=north west] {0};},
  ]
    \coordinate (0,0) ;
    % domain can be set individually!!
    \addplot[color=red, domain=0:10,samples=100,smooth,thick] {x} node[above,pos=1] {$45^\circ$};
    \addplot[color=blue,domain=0:11,samples=1000,smooth, thick] {3*sqrt(x)} node[right,pos=1] {$W(s,k_{t},\delta)$};
    \addplot[color=cyan,domain=0:11,samples=1000,smooth, thick] {2*sqrt(x)} node[right,pos=1] {$W(s',k_{t},\delta)$};

    \addplot[mark=*,only marks, fill=white] coordinates {(9,9)} node[above, pos=1]{$A$};
    \addplot[mark=*,only marks, fill=white] coordinates {(4,4)} node[above, pos=1]{$A'$};

    \addplot[mark=*,only marks, fill=white] coordinates {(9,6)} node[below, pos=1]{$A_1$};
    \addplot[mark=*,only marks, fill=white] coordinates {(6,6)} node[above, pos=1]{$A_2$};
    \addplot[mark=*,only marks, fill=white] coordinates {(6,4.899)} node[below, pos=1]{$A_3$};
    \addplot[mark=*,only marks, fill=white] coordinates {(4.899,4.899)} node[above, pos=1]{$A_4$};

    % NO output shown at all
    \node (A) at (9, 9){};
    \node (Ap) at (4, 4){};
    \node (A1) at (9, 6){};
    \node (A2) at (6, 6){};
    \node (A3) at (6, 4.899){};
    \node (A4) at (4.899, 4.899){};
    \draw [decorate,decoration={show path construction,
lineto code={\draw[dashed,-latex] (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast); }}]
(A) -- (A1) -- (A2) -- (A3) -- (A4);
  \end{axis}

\end{tikzpicture}
\end{center}
\end{document}

enter image description here