# Gap between line and point in pgfplots, like pointintervalbox in gnuplot

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

## 2 Answers

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,
legend image code/.code={
\draw [##1, shorten <=0cm] (0cm,0cm) -- (0.3cm,0cm);
\draw [only marks] plot coordinates {(0.3cm,0cm)};
\draw [##1, shorten >=0cm] (0.3cm,0cm) -- (0.6cm,0cm);
}}
\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,
legend image code/.code={
\draw [##1, shorten <=0cm] (0cm,0cm) -- (0.3cm,0cm);
\draw [only marks] plot coordinates {(0.3cm,0cm)};
\draw [##1, shorten >=0cm] (0.3cm,0cm) -- (0.6cm,0cm);
}}
\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}

• 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 '15 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 '15 at 9:41
• Sorry, I am having difficulties with the markup, since I want to add code and an image. I guess I have to open a new question. I will post a link here. – Rainer Nov 27 '15 at 11:27

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}


• 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 '15 at 12:49
• @Hotschke Did you see my sentence at the top? And it was also Jake and myself who pointed that out. – percusse May 18 '15 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 '15 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 '15 at 14:23