3

I want to place an arrow to indicate a remarkability in pgfplots. For now, I have the following code:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{decorations.markings}
\begin{document}
    \begin{tikzpicture}[baseline=(current axis.east)]
        \begin{axis}

           \addplot [only marks,thick,mark=x,mark size=4, color=red, mark size=6pt] coordinates {(-2,0) (0,0)};
           \addplot [blue,thick,postaction={decorate}, decoration={markings, mark=at position 0.85 with {\arrow{>};}}] coordinates {(-2,0) (-1,0)};
           \addplot [blue,thick,postaction={decorate}, decoration={markings, mark=at position 0.85 with {\arrow{>};}}] coordinates {(0,0) (-1,0)};
           \addplot [blue,thick,postaction={decorate}, decoration={markings, mark=at position 0.5 with {\arrow{>};}}] coordinates {(-1,0) (-1,-4)};
           \addplot [blue,thick,postaction={decorate}, decoration={markings, mark=at position 0.5 with {\arrow{>};}}] coordinates {(-1,0) (-1,4)};
        \end{axis}
    \end{tikzpicture}
\end{document}

I want it to add an arrow which can point to the cross to indicate something.

the desired arrow

1
  • 3
    Can you please make a compilable minimal working example which includes all libraries and packages necessary to compile your code? Jul 30 at 11:06

1 Answer 1

5

You can just use the axis coordinate system to draw inside the plot.

Inside the axis environment, the units for coordinates are different, so that (1,1) is bound to the units of the axes and does not mean (1cm,1cm) like it is outside the axis environment.

\documentclass[border=10pt]{standalone}
    
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{decorations.markings}
    
\begin{document}
    
    \begin{tikzpicture}[baseline=(current axis.east)]
        \begin{axis}
            \addplot [only marks,thick,mark=x,mark size=4, color=red, mark size=6pt] coordinates {(-2,0) (0,0)};
            \addplot [blue,thick,postaction={decorate}, decoration={markings, mark=at position 0.85 with {\arrow{>}}}] coordinates {(-2,0) (-1,0)};
            \addplot [blue,thick,postaction={decorate}, decoration={markings, mark=at position 0.85 with {\arrow{>}}}] coordinates {(0,0) (-1,0)};
            \addplot [blue,thick,postaction={decorate}, decoration={markings, mark=at position 0.5 with {\arrow{>}}}] coordinates {(-1,0) (-1,-4)};
            \addplot [blue,thick,postaction={decorate}, decoration={markings, mark=at position 0.5 with {\arrow{>}}}] coordinates {(-1,0) (-1,4)};

            \draw[<-, red, thick, shorten <=10pt] (-2,0) -- ++(1cm,1cm) node[above] {$K = 1$};

        \end{axis}
    \end{tikzpicture}
    
\end{document}

enter image description here

3
  • Thank you for your answer. Could you also show me how to add the text next to this arrow? Jul 30 at 11:14
  • Minor corrections - There should not be a ; in \arrow{>};. axis cs: is the default coordinate system since PGFPlots 1.11, so there is no need to write it explicitly. Jul 30 at 18:07
  • @hpekristiansen Thanks! Corrected and edited according to your suggestions. Jul 30 at 18:11

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.