6

I want to create a figure with 3 plots, a vertical reference line, and marks at the intersections of each plot with the reference line. The plots come from an external table.

Plotting works, but naming the plot for usage with the intersections library does not. Here is an example:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
  \begin{loglogaxis}[log basis x=10,log basis y=10]
    \addplot[name path=SProPath] coordinates {(1e-1,1e-7) (1e1,1e3)};
    \node[coordinate] (Ref) at (axis cs:1,1) {};
    \node[coordinate] (RefPathStart) at (Ref |- current axis.south east) {};
    \node[coordinate] (RefPathEnd) at (Ref |- current axis.north east) {};
    \path[name path=RefPath] (RefPathStart) -- (RefPathEnd);
    \fill [red,opacity=0.5,name intersections={of=SProPath and RefPath}]
      (intersection-1) circle (2pt);
  \end{loglogaxis}
\end{tikzpicture}
\end{document}

I'm getting the following errors:

! Package tikz Error: I do not know the path named `SProPath'. Perhaps you misspelt it.
! Package pgf Error: No shape named intersection-1 is known.

The path SProPath should be known to tikz. Is my way of naming the plot wrong? How can I create intersection between "drawn" paths and plots?

Christoph
  • 2,913

1 Answers1

6

As has been said in the comments, \addplot commands are executed in their own scope, so the name given to a path using name path will be lost immediately. To get around this, you can use name path global instead of name path, which will make the name available in all other scopes.

The same is true for the other path in your example: TikZ commands like \path and \draw inside an axis environment are not executed immediately, but only after the rest of the plot has been drawn. For this, each of the \path commands is wrapped in its own scope again. So you could either use name path global for the path as well, or you could pass the the \path and the \fill commands to a \pgfplotsextra{...} command. This will make sure both commands are executed in the same scope after the plot has been drawn, so name path global is not needed.

Jake
  • 232,450