# Positioning things with respect to axis of a pgfplot in tikz

I have a figure on an axis and want to add descriptive text and signs to it with tikz. However, I need to do the alignment of the text with respect to the axis by visually observing if it matches. Is there a way to tell tikz to use the alignment with respect to the axis. I tried using (axis cs:x,y) command but did not help.

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=10cm,
height=4cm,
x axis line style={-stealth},
y axis line style={-stealth},
title={this is my title},
xticklabels={},
ymax = 1.5, xmax=7.5,
axis lines*=center,
ytick={1},
xlabel={f},
ylabel={H(f)},
xlabel near ticks,
ylabel near ticks,
every axis x label/.style={at={(current axis.right of origin)},anchor=west},
every axis y label/.style={at={(ticklabel* cs:1.05)},anchor=south}
]
coordinates
{(0,0) (4,1) (5,0) (7,0)};
\end{axis}
% I want this to be aligned with axis' 4 and 5
\draw[<->] (4.0,-0.3) -- (5.0,-0.3)
\end{tikzpicture}
\end{document}

• To use axis cs (which is the default with compat=1.11 or newer I think), you hace to place the \draw command inside the axis environment. – Torbjørn T. Jun 1 '15 at 20:09
• Thanks a lot! It indeed works as expected. However, the figure is not automatically resized when draw command draws below axis. Is there a right way of doing it other than manually setting the height? – dagcilibili Jun 1 '15 at 20:15
• Thank you very much for your help found the answer to the second question and wanted to write here for completeness. Adding clip=false to the axis properties solves that issue too. – dagcilibili Jun 2 '15 at 17:49

Using axis cs outside an axis environment will not work, as there is no current axis. Hence, just move the \draw command inside the axis and add axis cs: and it will align as you want it. Since version 1.11 of pgfplots the axis cs coordinate system is used by default, if compat=1.11 (or higher) is specified in \pgfplotsset.

As you saw, objects placed outside the axis area are clipped away by default. You can disable clipping by adding clip=false to the axis options, but this will also disable clipping of plots that go outside the defined axis limits (xmin, xmax, ymin, ymax). If you instead introduce clip mode=individual, plots will be clipped, while additional \draw commands and similar will not be clipped.

\documentclass[border=5pt,tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=10cm,
height=4cm,
x axis line style={-stealth},
y axis line style={-stealth},
title={this is my title},
xticklabels={},
ymax = 1.5, xmax=7.5,
axis lines*=center,
ytick={1},
xlabel={f},
ylabel={H(f)},
xlabel near ticks,
ylabel near ticks,
every axis x label/.style={at={(current axis.right of origin)},anchor=west},
every axis y label/.style={at={(ticklabel* cs:1.05)},anchor=south},
clip mode=individual
]
coordinates
{(0,0) (4,1) (5,0) (7,0)};

\draw[<->] (axis cs:4.0,-0.3) -- (axis cs:5.0,-0.3);
\end{axis}
\end{tikzpicture}

% with compat=1.11 or newer the axis cs coordinate system is used by default
\pgfplotsset{compat=1.11}

\begin{tikzpicture}
\begin{axis}[
width=10cm,
height=4cm,
x axis line style={-stealth},
y axis line style={-stealth},
title={this is my title},
xticklabels={},
ymax = 1.5, xmax=7.5,
axis lines*=center,
ytick={1},
xlabel={f},
ylabel={H(f)},
xlabel near ticks,
ylabel near ticks,
every axis x label/.style={at={(current axis.right of origin)},anchor=west},
every axis y label/.style={at={(ticklabel* cs:1.05)},anchor=south},
clip mode=individual
]