[Tex/LaTex] Declare nodes in pgfplots as overlays

nodespgfplots

The Background

In pgfplots, it is possible to make plot elements such as axis labels, ticks and legends overlay, which means they will not be considered when determining the bounding box of the plot.

Using nodes is a good way of labeling points of interest in a plot, however if a node is automatically positioned beyond the bounding box of a plot, it will be clipped.

The Question

Is it possible in some way to set nodes to behave as if they are set as overlay? The node style options do not seem to respond to this keyword. Alternatively, is it possible to simply tell pgfplots not to clip the figure at the edges of the bounding box described by the plot area?

Things I've Considered

Which are nevertheless not what I am looking for:

  • Extend the plot using enlargelimits: adds more empty space to the plot area, and also lowers the y-axis baseline below zero, which is inappropriate when the dataset I'm graphing asymptotically approaches zero.

  • Extend the plot using manual xmin, xmax: Also not desired in this case for a similar reason as above.

  • Situational re-orientation of the node with respect to the coordinate of interest: I would like consistent orientation of the nodes.

Minimal Example

(The dataset comes from the pgfplots manual)

\documentclass{minimal}
\usepackage{pgfplots}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[]
\tikzstyle{every pin}=[draw=none,fill=white]
\begin{axis}[
width=10cm,
height=5cm,
axis x line*=bottom,
xlabel={$x$},
axis y line*=left,
ylabel={$y$},
grid=none
]
\addplot[color=black,mark=none] coordinates {
(-4.77778,2027.60977)
(-3.55556,347.84069)
(-2.33333,22.58953)
(-1.11111,-493.50066)
(0.11111,46.66082)
(1.33333,-205.56286)
(2.55556,-341.40638)
(3.77778,-1169.24780)
(5.00000,-3269.56775)
};
\node[coordinate,pin=above right:{node}] at (axis cs:3.77778,-1169.24780) {};
\end{axis}
\end{tikzpicture}
\end{document}

Reference Image

Reference rendering of a pgfplots chart with clipped node text.

Thanks for any help anyone can offer.

Best Answer

You can add the option clip=false,enlargelimits=false to the axis environment to prevent the clipping of nodes at the edge of the axis.

\documentclass{minimal}
\usepackage{pgfplots}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[]
\tikzstyle{every pin}=[draw=none,fill=white]
\begin{axis}[
width=10cm,
height=5cm,
axis x line*=bottom,
xlabel={$x$},
axis y line*=left,
ylabel={$y$},
grid=none,
clip=false,
enlargelimits=false
]
\addplot[color=black,mark=none] coordinates {
(-4.77778,2027.60977)
(-3.55556,347.84069)
(-2.33333,22.58953)
(-1.11111,-493.50066)
(0.11111,46.66082)
(1.33333,-205.56286)
(2.55556,-341.40638)
(3.77778,-1169.24780)
(5.00000,-3269.56775)
};
\node[coordinate,pin=above right:{node}] at (axis cs:3.77778,-1169.24780) {};
\end{axis}
\end{tikzpicture}
\end{document}

pgfplots without clipping

Alternatively, you can also add the annotations after the plot itself is finished, using the after end axis/.code key that has to be supplied to the axis. The advantage of this approach is that the clipping behaviour of the axes remains unchanged, so markers can still be clipped.

\documentclass{minimal}
\usepackage{pgfplots}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[]
\tikzstyle{every pin}=[draw=none,fill=white]
\begin{axis}[
width=10cm,
height=5cm,
axis x line*=bottom,
xlabel={$x$},
axis y line*=left,
ylabel={$y$},
grid=none,
enlargelimits=false,
after end axis/.code={\node[coordinate,pin=above right:{node}] at (axis cs:3.77778,-1169.24780) {};}
]
\addplot[color=black,mark=none] coordinates {
(-4.77778,2027.60977)
(-3.55556,347.84069)
(-2.33333,22.58953)
(-1.11111,-493.50066)
(0.11111,46.66082)
(1.33333,-205.56286)
(2.55556,-341.40638)
(3.77778,-1169.24780)
(5.00000,-3269.56775)
};
\end{axis}
\end{tikzpicture}
\end{document}