PGFPlots Data – Handling Missing Values in Table Data for PGFPlots

pgfplots

I am using PGFPlot with external table data. However, some values are missing for some plots. This causes data in column 2 does not exists error. Is there a way to solve this. The value does not exists and all data for x, ind3 should be considered as {(1,7)(3,15)}. Is there a way to overcome this problem?

The following is the data file I tried to create:

x   ind2   ind3
1   5      7
2   9      
3   11     15

And plot commands

\addplot[color=blue, mark=x] table[x=x, y=ind2] {performance.data};

\addplot[color=red, mark=+] table[x=x, y=ind3] {performance.data};

Best Answer

As you noted yourself, using nan as a placeholder for empty values solves the problem. It's worth pointing out that you also have the choice of whether to just skip the empty value and connect the surrounding data points, or whether the graph should be interrupted when it encounters an empty value. You can control this behaviour using unbounded coords=discard (which is the default behaviour) or unbounded coords=jump.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents}{performance.data}
x   ind2   ind3
0  2     4
1  5     7
2  9 nan
3 11  15
4 10  15
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\begin{axis}[ymax=20]
    \addplot table {performance.data};
    \addplot table [y=ind3] {performance.data};
    \addplot +[unbounded coords=jump,yshift=0.5cm] table [y=ind3] {performance.data}; % Plot shifted upwards to show difference
\end{axis}
\end{tikzpicture}
\end{document}
Related Question