[Tex/LaTex] Use pgfplots \addplot plot coordinates with relative coordinates

pgfplots

I want to use in pgfplots \addplot plot cordinates with relative coordinates. However the following syntax doesn't work:

\documentclass{article}

\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
   \begin{axis}
      \addplot plot coordinates {(0,0) ++(2,2) ++(1,3)};
   \end{axis}
\end{tikzpicture}

\end{document}

How can I make this (or something similar) work?

Best Answer

If you provide your data as a table instead of as a list of coordinates, you can use pgfplotstable to define a new column that consists of the accumulated x and y values:

enter image description here

\documentclass{article}

\usepackage{pgfplots, pgfplotstable}
\begin{document}

\pgfplotstableset{
    create on use/x rel/.style={
        create col/expr={
            \pgfmathaccuma + \thisrow{0}
        }
    },
    create on use/y rel/.style={
        create col/expr={
            \pgfmathaccuma + \thisrow{1}
        }
    }
}

\begin{tikzpicture}
    \begin{axis}
        \addplot table [x = x rel, y=y rel] {
      0 0
      2 2
      1 3
     };
   \end{axis}
\end{tikzpicture}

\end{document}