Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am creating plots that contain a LOT of data points, and each plot also includes a line that is independent but coincident with those points.

The problem is that the marks get in front of the line, so in regions of the plot where there are too many marks the line is not clearly visible. The following MWE contais a plot with fewer points and another with a lot of points to illustrate this.

\documentclass[12pt]{article}

\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=newest}

\pgfplotscreateplotcyclelist{marks}{%
  {black!50,ultra thin,mark=o,only marks},  
  {black,very thick,mark=none},
  {blue!70!black!50,mark=square,only marks},
  {blue!70!black,very thick,mark=none},
  {red!70!black!50,mark=triangle,only marks},
  {red!70!black,very thick,mark=none},
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[legend entries={0,...,6},
    legend pos=outer north east,
    cycle list name=marks]
\addplot+[samples=10] {x};
\addlegendentry{10 samples}
\addplot {x};
\addlegendentry{}
\addplot+[samples=100] {x-2};
\addlegendentry{100 samples}
\addplot {x-2};
\addlegendentry{}
\addplot+[samples=200] {x-4};
\addlegendentry{200 samples}
\addplot {x-4};
\addlegendentry{}
\end{axis}
\end{tikzpicture}

\end{document}

With this result:

The line should be in front

I have tried putting the line plots before the scatter plots but that didn't work. How can I make the line to be clearly visible, in front of the marks?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

This is similar to error bars with pgfplots in foreground: By default, all the markers are drawn in one layer on top of the plots. This is done so clipping for them can be switched on or off independently from the normal clipping. You can tell PGFPlots to use an individual layer for each set of markers, instead of putting them all on the same topmost layer, by setting clip mode=individual. This means that later plots will be drawn on top of the preceding markers.

\documentclass[12pt]{article}

\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=newest}

\pgfplotscreateplotcyclelist{marks}{%
  {black!50,ultra thin,mark=o,only marks},  
  {black,very thick,mark=none},
  {blue!70!black!50,mark=square,only marks},
  {blue!70!black,very thick,mark=none},
  {red!70!black!50,mark=triangle,only marks},
  {red!70!black,very thick,mark=none},
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    clip mode=individual,
    legend entries={0,...,6},
    legend pos=outer north east,
    cycle list name=marks]
\addplot+[samples=10] {x};
\addlegendentry{10 samples}
\addplot {x};
\addlegendentry{}
\addplot+[samples=100] {x-2};
\addlegendentry{100 samples}
\addplot {x-2};
\addlegendentry{}
\addplot+[samples=200] {x-4};
\addlegendentry{200 samples}
\addplot {x-4};
\addlegendentry{}
\end{axis}
\end{tikzpicture}

\end{document}
share|improve this answer
    
Thank you @Jake, it sounds like a great solution, but pdflatex is return an error: Package pgfkeys Error: I do not know the key '/tikz/clip mode'. I cannot find what library that option is implemented in. –  Francisco Nov 28 '12 at 15:51
    
@Francisco: Ah yes, that option was only implemented in a reasonably recent version of PGFPlots. I'd recommend you update your PGFPlots package. Alternatively, you can set clip=false instead of clip mode, which will work in this case, but you'll run into problems if you want to adjust your axis limits to ignore some of your data, as they will no longer be clipped. –  Jake Nov 28 '12 at 15:54
    
Still not working. clip=false produces no changes, and neither does clip marker paths=false. –  Francisco Nov 28 '12 at 16:20
    
Nevermind that, installing pgfplots locally worked like a charm. Thanks a lot! –  Francisco Nov 28 '12 at 16:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.