# Adding node to a pgfplots-plot

I have the following plot in TikZ:

\begin{tikzpicture}[trim axis left]
\begin{axis}[domain=2:6,
samples=100,
enlarge x limits=false,
no markers,
ymin=-0.5,ymax=1,
axis lines=left,xtick=\empty,ytick=\empty]
\node [left]  at (3,0) {$F$};
\end{axis}
\end{tikzpicture}


As you can see, I have tried to add a node at the coordinate (3,0), however, it isn't showing. What am I missing to make it appear?

• put \node [left] at (3,0) {$F$}; after end{axis} Mar 30, 2021 at 15:27
• I'm guessing you don't have compat=1.11 or higher, so the coordinates of the node are not that of the axis. Try (axis cs:3,0) instead of (3,0). Mar 30, 2021 at 15:28
• @TorbjørnT. That worked. But I am only able to place the node inside the coordinate system - not outside. Basically, what I want is for it to say F instead of 3 at the point x=3, i.e., to change the axis marker/label at x=3 Mar 30, 2021 at 15:33
• Oh, then you're going about it the wrong way, if I understood correctly. You want xtick=3,xticklabels=$F$ instead of xtick=empty. Mar 30, 2021 at 15:35
• @TorbjørnT. Very nice, thanks! Feel free to add an answer I can accept Mar 30, 2021 at 16:01

The reason you're not seeing the node, is that coordinates for \node, \draw and similar drawing macros from TikZ are not, by default, in the coordinates of the pgfplots axis. That, and the fact that pgfplots clips away everything outside the axis limits. So your node ends up outside the axis limits, and is clipped.

To actually use axis coordinates for a \node, you can define the coordinate system to use explicitly with axis cs::

\node at (axis cs:3,0) {..};


Alternatively axis cs can me made default, by adding

\pgfplotsset{compat=1.11}


or some higher version number. Currently the latest version i 1.17, so compat=1.17 is the highest valid version number. (It's probably a good idea to always specify the compat setting I think, for one thing even the lowest possible value (1.3) will improve the placement of the ylabel.)

However, based on the comments, what you're actually after is to add a custom ticklabel for x=3. The normal way to do that is to use xtick=3 in the axis options to specify that you want a tick at x=3, and then use xticklabels=$F$ to specify the text to use for the label.

More generally you can use comma separated lists in {} for both, e.g. xtick={3,7}, xticklabels={foo,bar}, or use the dots notation for ranges, e.g. 1,3,...,10 which gets you the odd numbers from 1 to 9.

A minimal example:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17} % it's recommended to use an explicit version number
\begin{document}
\begin{tikzpicture}[trim axis left]
\begin{axis}[domain=2:6,
samples=100,
enlarge x limits=false,
no markers,
ymin=-0.5,ymax=1,
axis lines=left,xtick=3,xticklabels=$F$,ytick=\empty]

% with the compat setting used above this adds the node at 3,0 in the axis
\node [left]  at (3,0) {$F$};

% while this would work regardless of the compat setting
\node [right]  at (axis cs:3,0) {$G$};
\end{axis}
\end{tikzpicture}
\end{document}

• Also the OP is probably affected by clipping. If you add clip mode = individual the manually added \draw and \node command are not clipped in the axis range. Mar 30, 2021 at 18:23
• @Rmano Yes, I did mention clipping, but I didn't think about clip mode, thanks :) Mar 30, 2021 at 18:25

You can define coordinates inside the axis environment and plot them outside to avoid clipping. The following assumes you want x=3 for [domain=2:6] but at the bottom edge of the plot.

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[trim axis left]
\begin{axis}[domain=2:6,
samples=100,
enlarge x limits=false,
no markers,
ymin=-0.5,ymax=1,
axis lines=left,xtick=3,xticklabels=$F$,ytick=\empty]
\node [left]  at (A|-B) {$F$};