1

I attempted to plot some Bézier curves in a pgfplots graph. This works well when the coordinates are positive. See:

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
 \begin{tikzpicture}
 \begin{axis}[ axis x line=middle, axis y line=middle,
 xmin = 0, xmax = 11, ymin = 0, ymax=4,
 unit vector ratio=1 1, grid,
 xlabel = $x$, ylabel = $y$]
   \draw[blue, thick] (axis cs:1,1.5)
   .. controls (axis cs:1.2,2.5) and +(axis cs:-0.5,0) ..
   (axis cs:2,3.5)coordinate(A)
   .. controls +(axis cs:0.5,0) and +(axis cs:-6,0) ..
   (axis cs:11,1.55);
   \draw[red,<->] (A) +(axis cs:-0.8,0) --+(axis cs:0.8,0);
 \end{axis}
\end{tikzpicture}

enter image description here

The problem is when I try the same thing for negative x coordinates (despite the use of axis direction cs syntax):

\begin{tikzpicture}
 \begin{axis}[axis x line=middle, axis y line=middle,
 xmin = -11, xmax = 0, ymin = 0, ymax=4,
 unit vector ratio=1 1, grid,
 xlabel = $x$, ylabel = $y$]
      \draw[blue, thick] (axis cs:-1,1.5)
   .. controls (axis direction cs:-1.2,2.5) and +(axis direction cs:0.5,0) ..
   (axis cs:-2,3.5)coordinate(B)
   .. controls +(axis direction cs:-0.5,0) and +(axis direction cs:6,0) ..
   (axis cs:-11,1.55);
   \draw[red,<->] (B) +(axis direction cs:-0.8,0) --+(axis direction cs:0.8,0);
 \end{axis}
\end{tikzpicture}

enter image description here

Any help on this would be appreciated. Thanks.

6
  • 1
    You are mixing axis direction cs: and axis cs: coordinates. – user121799 Jul 1 '19 at 13:22
  • @marmot I primarily used axis cs: and the result was incorrect. Reading the doc (part 4.17.1) I've found that axis direction cs: is dedicated to relative coordinates... with no success (the result is slightly different though). – Tobard Jul 1 '19 at 14:12
  • When I run your second example on my machine, I do not get the result you show. Please notice also that there is a common misconception: if you add ++ (axis cs:0,0) to a path this does not necessarily coincide with adding ++ (0,0)because the origin of the axis is in general not the origin of the embedding tikzpicture. – user121799 Jul 1 '19 at 14:29
  • Sorry, the published version of the code was not exactly the one that gave the image. Now it's correct and your primary comment was right : I've defined with axis direction an absolute coordinate. With this correction it's working as expected! Do I have to delete my question? – Tobard Jul 1 '19 at 14:35
  • No, you do not have to delete it. If you want you could write an answer which explains these pitfalls such that others learn from it. – user121799 Jul 1 '19 at 14:38
1

The solution was simple: as @marmot pointed out in the comments, there was a mistake: I was using axis direction cs in a non-relative coordinate definition. Applying this correction the code worked as expected:

\begin{tikzpicture}
 \begin{axis}[axis x line=middle, axis y line=middle,
 xmin = -11, xmax = 0, ymin = 0, ymax=4,
 unit vector ratio=1 1, grid,
 xlabel = $x$, ylabel = $y$]
    \draw[blue, thick] (axis cs:-1,1.5)
   .. controls (axis cs:-1.2,2.5) and +(axis direction cs:0.5,0) .. % replacement
   (axis cs:-2,3.5)coordinate(B)
   .. controls +(axis direction cs:-0.5,0) and +(axis direction cs:6,0) ..
   (axis cs:-11,1.55);
   \draw[red,<->] (B) +(axis direction cs:-0.8,0) --+(axis direction cs:0.8,0);
 \end{axis}
\end{tikzpicture}

enter image description here

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.