# Plotting list of points while ignoring missing or infinite values

My question is closely related to this one, which has not been answered yet and might get more attention in this more general formulation.

Suppose that a bunch of (abscissa, ordinate) points contains missing or infinite values which can be flagged somehow, for instance by directly having nan in the ordinate as I assume below, or by testing out-of-bounds values as in the above mentioned question. Is there a simple modification of \listplot which would plot a different curve for each group of contiguous values without missing/infinite value ?

Below is an illustration. First, I plot in black lines the data with no missing values. Then, I give the same points but with missing values; I use a trick redefining nan (or any user-defined flag) to simply get rid of them. However, the plot in red is not satisfying, because lines are connecting surrounding values. In blue, I plot "by hand" (i.e. by calling on several \listplot) the result I would like to get automatically; note that an isolated point can simply be discarded.

\begin{pspicture}
\psgrid[subgriddiv=1,griddots=10,gridlabels=7pt](0,0)(10,4)

\savedata{\data}[1 2  2 1  3 1  4 2    5 1    6 2  7 3  8 1    9 2  10 3]
\listplot[linecolor=black]{\data}

\savedata{\data}[1 2  2 1  3 1  4 nan  5 nan  6 2  7 3  8 nan  9 2  10 nan]
\pstVerb{/nan { pop } def} % nice trick to get rid of nan values
\pstScalePoints(1.,1.){}{.1 add} % some vertical shift to allow comparison
\listplot[linecolor=red]{\data}

\pstScalePoints(1.,1.){}{.2 add} % some vertical shift to allow comparison
\savedata{\data}[1 2  2 1  3 1]
\listplot[linecolor=blue]{\data}
\savedata{\data}[6 2  7 3]
\listplot[linecolor=blue]{\data}
% the following goes wrong because isolated point is completed with (0,0)
% \savedata{\data}[9 2]
% \listplot[linecolor=blue]{\data}
\end{pspicture} • Is pstricks a requirement, or would a pgfplots method be of interest? – Torbjørn T. Apr 12 '17 at 15:12
• this is supposed to be used within a long pstricks macro (and this is why I do not want to resort to splitting in several \listplot commands), so I'd rather have a version that works with pstricks. – Hugo Raguet Apr 12 '17 at 15:22
• I see, then I won't waste your time with that. – Torbjørn T. Apr 12 '17 at 15:25
• thanks anyway! (and if I do not find a reasonable pstricks way of doing it, I might consider shifting to other graphical packages around) – Hugo Raguet Apr 12 '17 at 15:27
• @Torbjørn T. I got no luck so far, and I am pretty sure such functionality would be useful to others than me. If you ever get the time to detail a pgfplot alternative, I would take it as an answer. – Hugo Raguet Apr 24 '17 at 7:21

pgfplots has this feature built in, with the option unbounded coords=jump.

I don't know where your data comes from. If you have them in a text file data.dat with the format

1 2
2 1
3 1
4 nan
etc.


then you can use that directly in the \addplot command, as \addplot [unbounded coords=jump] table {data.dat};. If you have multiple columns in a file, you can select which ones to use with e.g. \addplot table[x index=0,y index=2] {data.dat};.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
1 2
2 1
3 1
4 nan
5 nan
6 2
7 3
8 nan
9 2
10 nan
}\data
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% axis limits
xmin=0,xmax=10,ymin=0,ymax=4,
% size of plot
% scale only axis means that 10cm is only the axis, not the ticks
width=10cm,height=4cm,scale only axis,
grid=major
]
% plot data with gaps in line
\addplot [blue,very thick,unbounded coords=jump] table {\data};
% default setting, no gaps
\end{axis}
\end{tikzpicture}
\end{document} • Built-in... I really like pstricks but this goes definitely in favor of pgf + tickz. Thanks. – Hugo Raguet Apr 24 '17 at 9:39
\documentclass{scrartcl}
\usepackage{pst-plot}
\begin{document}

\begin{pspicture}[showgrid](10,4)
\savedata{\data}[1 2  2 1  3 1  4 2  5 1 6 2  7 3  8 1 9 2  10 3]
\listplot{\data}
\listplot[linecolor=blue,linewidth=2pt,nStart=1,nEnd=3]{\data}
\listplot[linecolor=blue,linewidth=2pt,nStart=6,nEnd=7]{\data}
\end{pspicture}

\end{document} • I should have emphasized the automatic part of the feature I am looking for; at least, without having to split manually into several \listplot commands. I am thinking right away of a command preprocessing the data and generating suitable \listplot with appropriate nStart and nEnd, but my knowledge of both latex and postscript is too poor to make such a command from scratch – Hugo Raguet Apr 13 '17 at 7:43