13

I am trying to draw a line between points of two plots as shown in the picture (the blue line) but without luck. It is with tikzpicture and pgfplots. Similar to:

\begin{tikzpicture}
\begin{axis}
  \addplot coordinates { (1,2) (2,3) };
  \addplot coordinates { (1,4) (2,6) };
\end{axis}
\end{tikzpicture}

I did try adding nodes to \addplot but they seem to be located at (0,0) regardless of settings.

Any suggestions?

20

The pgfplots package introduces an own coordinate system that allows you to use the diagram's coordinates with normal tikZ drawing commands. Just put an axis cs: before your coordinates as in

\draw (axis cs:1,0) -- (axis cs:2,0);

The following code below shows your code expanded with a line drawn from the points (2,3) to (2,6):

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}
    \addplot coordinates { (1,2) (2,3) };
    \addplot coordinates { (1,4) (2,6) };
    \draw (axis cs:2,3) -- node[left]{Text} (axis cs:2,6);
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Thanks! I really appreciate your help :-) saved me a lot of time. – Lasse Espeholt Oct 25 '13 at 10:29
  • With pgfplots version 1.11 you can instead \draw (axis cs:2,3) -- node[left]{Text} (axis cs:2,6); write \draw (2,3) -- node[left]{Text} (2,6);. For this you need to declare pgfplotsset{compat: 1.13} in document preamble (if you used the latest version of pgfplots). – Zarko Feb 25 '16 at 9:47
  • @Zarko Thanks for the hint. I can't check it myelf right now since I'm still on 1.10 for some legacy task that I have to finish and where I don't want to mess up with things by updating in the middle of the process. But is it really compat 1.13 if it is a new feature in version 1.11 or is this a typo? – Benedikt Bauer Feb 25 '16 at 10:05
  • @BenediktBauer, version 1.11 is not the last, so I in my comment I give example for the last version (since if you will upgrade pgfplots, you will get this one). It is worth to upgrade it (as soon as possible). – Zarko Feb 25 '16 at 10:15
  • 1
    @Zarko In general I would absolutely agree, but since am currently still on TexLive2013, upgrading would mean upgrading my whole TeX distribution anyways. With a dissertation that is some hours work away from final publication, I won't take the hassle of everything getting messed up. I have tried compiling it once on a computer with a newer TexLive distribution and it failed terribly. – Benedikt Bauer Feb 25 '16 at 10:35

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.