2

I don't seem to have a problem placing a coordinate in the axis coordinate system, and translating in the x direction seems to work as expected also. But as soon as I try to translate in the y direction, I get puzzled by the result:

\documentclass{article}
    \usepackage{pgfplots}
    \usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[grid=major,]
            \addplot plot coordinates{(1800,2.14) (1860,2.18)};
            \coordinate (bottomLeft) at (axis cs:1800,2.14);
            \coordinate (bottomRight) at ($(bottomLeft) + (30,0)$);
            \draw (bottomLeft) -- (bottomRight);
            \coordinate (topLeft) at ($(bottomLeft) + (0,0.02)$);
            \coordinate (topRight) at ($(topLeft) + (20,0)$);
            \draw[red,thick] (topLeft) -- (topRight);
        \end{axis}
    \end{tikzpicture}
\end{document}

generates

one

and shows that I translated in the x direction both times, but the y translation by (0,0.02) isn't calculated as I would have expected - I wanted a coordinate at (1800,2.16). You can skip to the bottom now unless you'd like to see my troubleshooting that didn't work.

I discovered that a calculation is happening with

\documentclass{article}
    \usepackage{pgfplots}
    \usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[grid=major,]
            \addplot plot coordinates{(1800,2.14) (1860,2.18)};
            \coordinate (bottomLeft) at (axis cs:1800,2.14);
            \draw[purple] ($(bottomLeft) + (0,0.2)$) circle (0.3cm);
            \filldraw[purple] ($(bottomLeft) + (0,10)$) circle (0.3cm);
        \end{axis}
    \end{tikzpicture}
\end{document}

because I was able to translate in the y direction - it just doesn't seem to be translating in the axis cs the same way I could in the x direction:

two

The open circle didn't look translated at all, but upon closer examination, I got the filled circle to translate (a distance seemingly unrelated to the axis cs). I thought maybe the y axis would be handled differently from the x axis, so I tried to specify axis cs again:

\documentclass{article}
    \usepackage{pgfplots}
    \usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[grid=major,]
            \addplot plot coordinates{(600,1) (1800,2.14) (1860,2.18)};
            \coordinate (bottomLeft) at (axis cs:1800,2.14);
            \coordinate (test) at ($(bottomLeft) + (axis cs:0,0.2)$);
            \draw[thick,purple] (test) circle (0.3cm);
            \draw[thick] (bottomLeft) -- (test);
        \end{axis}
    \end{tikzpicture}
\end{document}

but changing the scale (by changing the lower left point) shows that (test) doesn't stay in the same place relative to the axis cs:

threeA threeB

I tried specifying axis cs for the whole calculation and with different bracket locations, but none would even compile.

\documentclass{article}
    \usepackage{pgfplots}
    \usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[grid=major,]
            \addplot plot coordinates{(800,1) (1800,2.14) (1860,2.18)};
            \coordinate (bottomLeft) at (axis cs:1800,2.14);
            %\coordinate (test) at (axis cs:$(bottomLeft) + (0,0.2)$);%didn't compile
            %\coordinate (test) at (axis cs:{$(bottomLeft) + (0,0.2)$});%didn't compile
            %\coordinate (test) at (axis cs:$(bottomLeft) + {(0,0.2)}$);%didn't compile
            %\coordinate (test) at (axis cs:${(bottomLeft) + (0,0.2)}$);%didn't compile
            \coordinate (translation) at (axis cs:0,0.2);
            \coordinate (test) at ($(bottomLeft) + (translation)$);
            \draw[thick,purple] (test) circle (0.3cm);
            \draw[thick] (bottomLeft) -- (test);
        \end{axis}
    \end{tikzpicture}
\end{document}

When I tried creating both coordinates in advance of the calculation, (test) still doesn't appear to move with the axis cs:

fourA fourB

Does anyone know how I can calculate a coordinate location using two other coordinates which have been defined in the axis coordinate system?

  • 1
    If you use \pgfplotsset{compat=1.11} then you should not need to specify axis cs:. – Peter Grill Feb 21 '15 at 5:42
  • @PeterGrill how's that? in my actual document i'm using compat=1.7 but i didn't include that in the mwe. – aeroNotAuto Feb 21 '15 at 21:35
3

As noted in the pgfplots manual:

As noted in the documentation for axis cs, adding two coordinates by means of the TikZ ++ operator1 may have unexpected effects. The correct way for ++ operations is axis direction cs.

1 While you're not strictly using ++ here, + from the calc library is effectively the same as far as this is concerned.

To do relative coordinate transformation, use axis direction cs instead of axis cs:

\documentclass{standalone}
    \usepackage{pgfplots}
    \pgfplotsset{compat=1.12}
    \usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[grid=major,]
            \addplot plot coordinates{(1800,2.14) (1860,2.18)};
            \coordinate (bottomLeft) at (axis cs:1800,2.14);
            \coordinate (bottomRight) at ($(bottomLeft) + (axis direction cs:30,0)$);
            \draw (bottomLeft) -- (bottomRight);
            \coordinate (topLeft) at ($(bottomLeft) + (axis direction cs:0,0.02)$);
            \coordinate (topRight) at ($(topLeft) + (axis direction cs:20,0)$);
            \draw[red,thick] (topLeft) -- (topRight);
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

|improve this answer|||||

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.