4

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]
\addplot +[thick, black] {ln(4/x)};
\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?

Torbjørn T.
  • 206,688
Tyler D
  • 321

2 Answers2

3

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]
\addplot +[thick, black] {ln(4/x)};

% 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}

Torbjørn T.
  • 206,688
  • 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. – Rmano Mar 30 '21 at 18:23
  • 1
    @Rmano Yes, I did mention clipping, but I didn't think about clip mode, thanks :) – Torbjørn T. Mar 30 '21 at 18:25
1

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] \addplot +[thick, black] {ln(4/x)}; \coordinate (A) at (axis cs:3,0);% y value unimportant \coordinate (B) at (rel axis cs: 0,0);% lower left corner \end{axis} \node [left] at (A|-B) {$F$}; \end{tikzpicture} \end{document}

demo

John Kormylo
  • 79,712
  • 3
  • 50
  • 120