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
    
This solution has issues with overlapping curves (illustrated here tex.stackexchange.com/questions/245526/…). However, it has been pointed out that it should be faster than the answer by Jake. –  Hotschke May 18 at 12:49
1  
@Hotschke Did you see my sentence at the top? And it was also Jake and myself who pointed that out. –  percusse May 18 at 14:13
    
Do you mean the sentence about varying background color? Yes, I saw this. However, I think the more common case are intersecting curves instead of varying background colors. I just wanted to make it more explicit. Sorry if I seem to have ignored your sentence or the comments. I didn't want to be offensive. Originally, I was looking for a gnuplot solution, but was quite surprised that the author of the gnuplot solution didn't point it out. Or maybe I am even more surprised that there is no possibility in gnuplot at all. Anyhow thanks for setting this right. –  Hotschke May 18 at 14:21
    
@Hotschke No problem. I just thought you missed my nickname in those comments above. Another way is to use a scatter plot then connect them artificially. Would be also slow but it is an alternative. –  percusse May 18 at 14:23

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 '14 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 '14 at 18:56
    
@Jake: I want to add a pin to a few data points. I am drawing with your discontinuous line style. However, I'd like to have the pin snap to the points. See e.g. tex.stackexchange.com/questions/191365/…. Do I have to add the markers a second time? –  Hotschke Jun 12 at 8:35
    
@Jake: I've noticed that using a cycle list is not working. Do I have to specify the color explicitly in the addplot command? –  Hotschke Jun 26 at 9:41
    
Another issue I have encountered is when trying to calculate intersections (e.g. tex.stackexchange.com/a/38572/8917). Without discontinuous line every thing works as expected. –  Hotschke Jul 30 at 8:13

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.