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 wondered if it is possible to create a scatter plot in pgfplots with a similar style as the "pointintervalbox" option in gnuplot, i.e. a plot with points and lines, but there is a gap between the lines and the points.

Here is an example:

Maybe something like this already exists but I couldn't find it because I don't really know how to describe it.

Thank you

share|improve this question

2 Answers 2

If you don't have varying background color, you can draw the markers with background color

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}
\begin{tikzpicture}
\begin{axis}[]
\addplot+[mark options={very thick,draw=white},samples=5] {rand};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer

In principle, you can use the mesh style for this: for line plots, this results in the line being drawn with individual segments, instead of using one continuous path. Then you can apply the shorten <=1mm, shorten >=1mm options to offset the start and end of the segments.

However, there's one implementation detail that hinders this: currently, PGFPlots uses \pgfusepathqstroke to draw the segments, which ignores all options like arrow tips or shortenings. So to make this work, we have to redefine an internal macro to instead use \pgfusepath{stroke}. Here's a key that takes care of that, and takes an optional argument for setting the gap length:

\makeatletter
\pgfplotsset{
    discontinuous line/.code={
        \pgfkeysalso{mesh, shorten <=#1, shorten >=#1}
        \def\pgfplotsplothandlermesh@VISUALIZE@std@fill@andor@stroke{%
            \pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{fill path}%
            \pgfplotsplothandlermesh@definecolor
            \pgfusepath{stroke}
            \pgfplotsplothandlermesh@show@normals@if@configured
        }%
    },
    discontinuous line/.default=1.5mm
}
\makeatother

Putting that code snippet in your preamble, after \usepackage{pgfplots}, allows you to get a discontinous line like this:

\begin{axis}
\addplot [discontinuous line, black, mark=*] table {
0 5
1 3
2 4
3 8
4 0
};

\addplot [discontinuous line=3mm, red, mark=*] table {
0 1
2 5.5
3 7.25
4 8
};
\end{axis}

Full code:

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}

\makeatletter
\pgfplotsset{
    discontinuous line/.code={
        \pgfkeysalso{mesh, shorten <=#1, shorten >=#1}
        \def\pgfplotsplothandlermesh@VISUALIZE@std@fill@andor@stroke{%
            \pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{fill path}%
            \pgfplotsplothandlermesh@definecolor
            \pgfusepath{stroke}
            \pgfplotsplothandlermesh@show@normals@if@configured
        }%
    },
    discontinuous line/.default=1.5mm
}
\makeatother

\begin{document}

\begin{tikzpicture}

\begin{axis}[
    axis background/.style={
        shade,bottom color=gray!50,top color=white
    }
]
\addplot [discontinuous line, black, mark=*] table {
0 5
1 3
2 4
3 8
4 0
};

\addplot [discontinuous line=3mm, red, mark=*] table {
0 1
2 5.5
3 7.25
4 8
};
\end{axis}
\end{tikzpicture}

\end{document}
share|improve this answer
1  
Haha, nice!! I was hoping that noone will go through this :P But for large sample sizes in other plots, you just earned another long coffee break. That really makes a difference. –  percusse Aug 2 at 18:27
    
@percusse: You're absolutely right. I've edited my answer to use a key instead, which makes it easier to keep the change local, so "real" mesh plots aren't affected and can keep using \pgfusepathqstroke. –  Jake Aug 2 at 18:56

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.