1

I would like to draw a path in a graph plotted with pgfplots, using relative (++) coordinates. The following code compiles without any error, but the points are not at the desired position:

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

\begin{document}

\begin{tikzpicture}
    \begin{axis}
     \addplot{x^2};
     \draw (1,2) --++ (0,1) --++ (1,0) --++ (0,1); % This should result in a staircase
    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here Any idea of what causes this problem?

  • I can't answer the question about the cause of the problem, but you might like to make the staircase with "const plot" instead. For example, try adding a new line: \addplot [red,const plot] {x^2}; – Thruston Jan 15 '17 at 14:20
  • Thank you, but I am not particularly interested in plotting a staircase: this is just an simple example of a path based on relative coordinates. – Tobard Jan 15 '17 at 14:27
1

As you can find in the PGFPlots manual in 4.17.1 on page 349 (v1.14) you need to use axis direction cs to make relative coordinates work.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    % use this `compat' level or higher so you don't need to state `axis cs:'
    % at the coordinates
    \pgfplotsset{compat=1.11}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}
            \addplot{x^2};
            \draw [red,very thick] (1,2)
                -- ++(axis direction cs:0,1)
                -- ++(axis direction cs:1,0)
                -- ++(axis direction cs:0,1)
            ;
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

  • For your information: Besides accepting the answer you could also upvote it by clicking on the the 'up' arrow above the answer's score. – Stefan Pinnow Jan 15 '17 at 15:30
  • axis direction cs or axis description cs ?? Your code differs from your description. – Thruston Jan 15 '17 at 18:04
  • @Thruston, sorry, my fault. Corrected my "typo". Thanks for the hint. – Stefan Pinnow Jan 15 '17 at 18:06

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.