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
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:
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:
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:
Does anyone know how I can calculate a coordinate location using two other coordinates which have been defined in the axis coordinate system?
Best Answer
As noted in the
pgfplots
manual:1 While you're not strictly using
++
here,+
from thecalc
library is effectively the same as far as this is concerned.To do relative coordinate transformation, use
axis direction cs
instead ofaxis cs
: