5

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?

1
7

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}
2
  • Thanks very much. When I add a % sign at the end of the table lines to prevent emacs to mash up my formatting, it doesn't compile. Do you have any tip for this?
    – student
    Sep 23 '12 at 17:13
  • 1
    You can add ignore chars=\% to the table options.
    – Jake
    Sep 23 '12 at 17:15

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.