[Tex/LaTex] PGFplots: Can I do vector addition/substraction

pgfplotstikz-pgf

I'd like, in the following MWE, to draw a vector from P to P+v and to P-v for some given point P and directional vector v in the axis coordinate system of a pgfplot. Can it be done?

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  axis equal,
  xmin=-4,
  xmax=4,
  ymin=-4,
  ymax=4,
]

\coordinate (P) at (axis cs:2,1);

\draw[->] (P) -- (axis cs:2+1,1+1); % P+v
\draw[->] (P) -- (axis cs:2-1,1-1); % P-v

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

This is being done in a pgfplot due to additional plotting commands that aren't shown.

Edit: From the manual, chapter 4.17:

This means, in particular, that
adding two points has unexpected effects: the expression (axis cs:0,0) ++ (axis cs:1,0) is not
necessarily the same as (axis cs:1,0).

Best Answer

You have to use the axis direction cs, described in section 4.17 of the pgfplots v 1.11 manual. I have also used the calc tizklibrary in this MWE. The calc library is necessary to be able to do the subtraction. Moreover, as I suggested in a comment to the question, the axis cs on the definition of P is not required in v1.11.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  axis equal,
  xmin=-4,
  xmax=4,
  ymin=-4,
  ymax=4,
]

\coordinate (P) at (2,1);
\coordinate (v) at (axis direction cs:1,1);

\draw[->] (P) -- ($(P)+(v)$); % P+v
\draw[->] (P) -- ($(P)-(v)$); % P-v
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here