[Tex/LaTex] how to plot error bars on top of previous data using pgfplots

pgfplots

I am trying to plot a set of points with error bars on top of a very dense scatter plot, and the error bars are hidden behind the data as shown in the attached figure.

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
  \begin{semilogyaxis}
    \addplot[blue,mark=x] table [x index=0,y index=1]
      {PSF_blue_0_prof.dat};
    \addplot[green,mark=x, error bars/.cd,y explicit,y dir=both]
      table [x index=0,y index=1,y error index=2]
      {PSF_blue_1.0_prof.dat};
  \end{semilogyaxis}
\end{tikzpicture}

\end{document}

enter image description here

Data files are available here:

The green data points are added in the end using error bars/.cd,y explicit,y dir=both but the error bars appear behind the previous blue and red data. How could I plot the error bars in this figure?

Best Answer

Possibility 1: clip mode=individual

You need to set clip mode=individual. The clip modecontrols whether markers are placed on top of everything else (clip mode=global) or are overdrawn by following plots (clip mode=individual). But some parts of the plot can overflow the axis boundaries which may be undesirable. From the pgfplots manual:

The choice global tells pgfplots to install one single clip path for the complete picture. In order to avoid clipped marker paths, any markers are processed after the clip path has been closed, i.e. on a separate layer (see clip marker paths). An unexpected side effect is that marks are on top of plots, even if the plots have been added after the markers. The choice individual instructs pgfplots to install a separate clip path for every \addplot command. Consequently, the plot will be clipped. But most importantly, its markers will be drawn immediately after the clip path has been deactivated.

enter image description here

Possibility 2: clip marker paths=true

Another option is to set clip marker paths=true instead. Then the plot will be clipped by the axis' boundaries. This option prevents an overflow of the plot over the axis' boundaries.

The initial choice clip marker paths=false causes markers to be drawn after the clipped region. Only their positions will be clipped. As a consequence, markers will be drawn completely, or not at all. The value clip marker paths=true is here for backwards compatibility: it does not introduce special marker treatment, so markers may be drawn partially if they are close to the clipping boundary. This key has no effect if clip=false. Note that clip marker paths also acts the sequence in which plots and their markers are drawn on top of each other.

enter image description here

Possibility 3: clip=false

Finally there is also the possibility to set clip=false. Then the entire plot won't be clipped by the axis' boundaries. This feature can be useful if you use only one x and one y axis.

enter image description here

MWE

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
  \begin{semilogyaxis}[
    % clip mode=individual,      % Possibility 1
    % clip marker paths=true,    % Possibility 2
    % clip=false,                % Possibility 3 
    ymin=1e-5
    ]
    \addplot[blue, mark=x] table [x index=0,y index=1]
      {PSF_blue_0_prof.dat};
    \addplot[green,mark=x, error bars/.cd,y explicit,y dir=both]
      table [x index=0,y index=1,y error index=2]
      {PSF_blue_1.0_prof.dat};
  \end{semilogyaxis}
\end{tikzpicture}

\end{document}