I'm trying to do some annotation with TikZ
in a PGFplots
axis environment by means of nodes. However, the placement of the nodes is going wrong all the time, see the minimum working example below. How can I fix this? How do I calculate the coordinates for the TikZ
annotation properly?
MWE:
\documentclass[margin=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
xtick={-0.1,0,1,1.1},xmin=-0.1,xmax=1.1,xlabel=$x$,
ytick={-0.2,0,2,2.2},ymin=-0.2,ymax=2.2,ylabel style={rotate=-90},ylabel=$y$,
unit vector ratio=2 1 1,
]
\addplot[blue,domain=0:1] {x};
\addplot[red,domain=0:1] {2*x};
% Annotation.
\coordinate (SW) at (axis cs:-0.1,-0.2); % (S)outh (w)est.
\coordinate (SE) at (axis cs:1.1,-0.2); % (S)outh (e)ast.
\coordinate (NE) at (axis cs:1.1,2.2); % (N)orth (e)ast.
\coordinate (NW) at (axis cs:-0.1,2.2); % (N)orth (w)est.
\coordinate (A) at (axis cs:1,0);
\fill[black] (A) circle (1pt) node[anchor=south]{A};
% ??? WHY IS THIS NOT WORKING. SHOULD BE THE SAME AS NODE A. ???
\coordinate (B) at ($(SE)+(axis cs:-0.1,0.2)$); % ??? WHY IS THIS NOT WORKING. SHOULD BE THE SAME AS NODE A. ???
\fill[black] (B) circle (1pt) node[anchor=south east]{B}; % ??? WHY IS THIS NOT WORKING. SHOULD BE THE SAME AS NODE A. ???
% ??? WHY IS THIS NOT WORKING. SHOULD BE THE SAME AS NODE A. ???
\end{axis}
\end{tikzpicture}
\end{document}
axis cs
coordinate system fornodes
?xmin
andymin
values (with possiblyclip=false
added) and you'll see that B would be dancing around.$$
in($(1,0)$)
, just write(1,0)
.axis cs
is the default within the axis environment, according to thePGFplots
manual. See4.17 Custom annotations
,PGFplots
manual Revision 1.12 (2015/01/31). Nonetheless, it is indeed more consistent to useaxis cs
everywhere, so I edited the code ;). Thanks for your suggestion.pgfplots
and not only positioning nodes.