# Leave out every nth connection in pgfplots

Consider the following MWE:

\documentclass{article}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot coordinates {(4,0) (3,3) (3,0) (2,2) (2,0) (1,1)};
\end{axis}
\end{tikzpicture}
\end{document}


which produces the this plot:

However I don't want all data points to be connected. Instead I want to leave out every second connection which in this example are the vertical connections between the same x-coordinates. So in a more generalized form I am asking for a way to leave out every nth connection in a diagram.

PS:
I need the solution to work with a \addplot table{...} command which means that the input data is more or less fixed and I'd prefer a way to tell pgfplots to decide where to connect instead of modifying the input data file.

As described on p. 45 of the manual, all you have to do is to switch to version 1.4 or later, and to add an empty line where you want to have the jump.

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot coordinates {(4,0) (3,3) (3,0) (2,2)

(2,0) (1,1)};
\end{axis}
\end{tikzpicture}
\end{document}


This works the same way for tables.

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}
X Y
4 0
3 3

3 0
2 2

2 0
1 1
};
\end{axis}
\end{tikzpicture}
\end{document}


As for the update of your question, here is a way to skip every nth connection.

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16,
scatter,only marks,mark=none,
scatter/position=absolute,
scatter/@pre marker code/.code={
\node[circle,fill,inner sep=2pt] (pt-\pgfkeysvalueof{/data point/index}) at
(\pgfkeysvalueof{/data point/x},\pgfkeysvalueof{/data point/y}){};},
scatter/@post marker code/.code={%
\ifnum\pgfkeysvalueof{/data point/index}=0
\else
\pgfmathtruncatemacro{\itest}{mod(\pgfkeysvalueof{/data point/index},#1)}
\ifnum\itest=0
\else
\pgfmathtruncatemacro{\lastindex}{\pgfkeysvalueof{/data point/index}-1}
\draw (pt-\lastindex) -- (pt-\pgfkeysvalueof{/data point/index});
\fi
\fi
}}}
\begin{document}
\begin{tikzpicture}
\begin{axis}
X Y
4 0
3 3
3 0
2 2
2 0
1 1
};
\end{axis}
\end{tikzpicture}
\end{document}


If you want to skip every 3rd connection, use \addplot[discard every=3,blue] table {.... instead.

Note, however, that the marks are now defined in the scatter code.

• Is there a way to warp this into a \addplot table{...} command? Like inserting empty rows on the fly? – Raven Dec 16 '18 at 15:20
• @Raven Yes, sure. The example on p. 45 uses a table. I just "backtranslated" this to the case of a coordinate list in my answer. – user121799 Dec 16 '18 at 15:22
• Okay then I'll have a look at it there. Take you :) – Raven Dec 16 '18 at 15:31
• Unfortunately this is not exactly what I was hoping for. I'd prefer to not have to change my input table in which there are no empty rows. I'll have to check whether one cam do something with the output empty row key from pgfplotstable... – Raven Dec 16 '18 at 15:53
• Yes that appears to be exactly what I am searching for. I'll play around with it shortly but I am very optimistic that this satisfies all my needs. Thank you very much! – Raven Dec 16 '18 at 16:50