[Tex/LaTex] How to make label (node) to appear in front of markers in pgfplots

pgfplots

How do I make label (node) to appear in front of markers in pgfplots?

For example in:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot coordinates {
    (1, 1)
}  node[pos=0.5]{\color{green}{LABEL}};
\end{axis}
\end{tikzpicture}
\end{document}

Note that "LABEL" appears behind the plot marker:

dotlabel

It might be a good default (or inherited from TikZ) but how do I change this behavior? (Preferably something that still allows using the ...} node[]{...};-syntax.)

Best Answer

Normally markers are drawn on top of all plots in an axis environment (clip mode=global, default) or at least on top of the single plot (clip mode=individual) to avoid clipping the markers. In your code the node is part of the plot path and therefore behind the markers.

Version 1

You can shift the markers in the layer axis tick labels which is behind of the main layer:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
  set layers,% using layers
  mark layer=axis tick labels% defines the layer of the marks
}
\begin{axis}
\addplot coordinates {
   (0.5,0.5) (1, 1) (1.5,1.5)
  } node[pos=0.5]{\color{green}{LABEL}};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Note that then all markers will be behind all plots in the same axis environment.

Version 2

Set a coordinate in the plot path and draw the node in a pgfonlayer environment on the axis foreground layer:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{set layers}% using layers
\begin{axis}[set layers]
\addplot coordinates {
     (0.5,0.5) (1, 1) (1.5,1.5)
  } coordinate[pos=0.5](p1);% coordinate in the middle of the plot path
\begin{pgfonlayer}{axis foreground}
  \node at (p1){\color{green}{LABEL}};% draw the node in the axis foreground
\end{pgfonlayer}
\end{axis}
\end{tikzpicture}
\end{document}

Unfortunately I don't know how to change the layer for the node directly in their options. But maybe there is such a possibility.

Version 3

Use clip mode=invidual, set a coordinate in the plot path and draw the node when the plot is finished:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[clip mode=individual]
\addplot coordinates {
   (0.5,0.5) (1, 1) (1.5,1.5)
} coordinate[pos=0.5](p1);% coordinate in the middle of the plot path
\node at (p1){\color{green}{LABEL}};
\end{axis}
\end{tikzpicture}
\end{document}

Edit

From the manual v. 1.10, page 349:

The layers/standard layer configuration is defined by

\pgfplotsset{
  layers/standard/.define layer set=
    {axis background,axis grid,axis ticks,axis lines,axis tick labels,main,%
      axis descriptions,axis foreground}
    {
      grid style= {/pgfplots/on layer=axis grid},
      tick style= {/pgfplots/on layer=axis ticks},
      axis line style= {/pgfplots/on layer=axis lines},
      label style= {/pgfplots/on layer=axis descriptions},
      legend style= {/pgfplots/on layer=axis descriptions},
      title style= {/pgfplots/on layer=axis descriptions},
      colorbar style= {/pgfplots/on layer=axis descriptions},
      ticklabel style= {/pgfplots/on layer=axis tick labels},
      axis background@ style={/pgfplots/on layer=axis background},
      3d box foreground style={/pgfplots/on layer=axis foreground},
    },
}

You can define a new layer set and for example add a new layer behind main:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\pgfplotsset{
  layers/mylayers/.define layer set=
    {axis background,axis grid,axis ticks,axis lines,axis tick labels,%
      axis mymarks,% additional layer behind main
      main,axis descriptions,axis foreground}
    {/pgfplots/layers/standard}% re-uses the style definitions of layers/standard
}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
  set layers=mylayers,% using mylayers
  mark layer=axis mymarks% defines the layer of the marks
}
\begin{axis}
\addplot coordinates {
   (0.5,0.5) (1, 1) (1.5,1.5)
  } node[pos=0.5]{\color{green}{LABEL}};
\end{axis}
\end{tikzpicture}
\end{document}