3

I need to plot the content of a csv file. The table in the file contains several empty cells which should be replaced by NaNs so that, plotting the data with the unbounded coords=jump option would interrupt the plot at the positions of the missing values.

I thought of using the empty cells with key, but it doesn't work as expected. In the following example, the first table contains empty cells which are automatically replaced by NaNs, and doesn't work; the second table already contains NaNs cells and works properly instead.

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.14}
\usepgfplotslibrary{groupplots}
\usepackage{filecontents}

% the next table won't work properly
\begin{filecontents}{testtablea.csv}
a;b
0;0
1;1
2;2
3;2
4;2
5;2
6;
7;
8;2
9;1
10;0
\end{filecontents}

% this will work instead
\begin{filecontents}{testtableb.csv}
a;b
0;0
1;1
2;2
3;2
4;2
5;2
6;NaN
7;NaN
8;2
9;1
10;0
\end{filecontents}

\pgfplotstableset{empty cells with={NaN}}
\pgfplotstableread[col sep=semicolon]{testtable1.csv}\testtablea
\pgfplotstableread[col sep=semicolon]{testtable2.csv}\testtableb
\begin{document}
\centering
\pgfplotstabletypeset{\testtablea}\hspace{3cm}
\pgfplotstabletypeset{\testtableb}

\begin{tikzpicture}
\begin{axis} [title=automatic replacement, anchor=north east, width=7cm]
\addplot+ [unbounded coords=jump] table {\testtablea};
\end{axis}
\hspace{1cm}

\begin{axis} [title=well defined table, anchor=north west, width=7cm]
\addplot+ [unbounded coords=jump] table {\testtableb};
\end{axis}
\end{tikzpicture}
\end{document}

wrong and correct behaviour

So the question is:

1) how can I replace the empty cells in the original csv file by means of pgfplotstable to reach my goal?

or

2) how can I directly skip the empty values in a pgfplot (that is without pgfplotstable)?

1 Answer 1

5

With package ifthen you could add

y filter/.code={\ifthenelse{\equal{#1}{}}{\def\pgfmathresult{nan}}{}}

as plot option or axis option.

enter image description here

Code:

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.14}
\usepackage{ifthen}% <- added
% the next table won't work properly
\begin{filecontents}{testtablea.csv}
a;b
0;0
1;1
2;2
3;2
4;2
5;2
6;
7;
8;2
9;1
10;0
\end{filecontents}

% this will work instead
\begin{filecontents}{testtableb.csv}
a;b
0;0
1;1
2;2
3;2
4;2
5;2
6;NaN
7;NaN
8;2
9;1
10;0
\end{filecontents}

\pgfplotstableset{empty cells with={NaN}}
\pgfplotstableread[col sep=semicolon]{testtablea.csv}\testtablea
\pgfplotstableread[col sep=semicolon]{testtableb.csv}\testtableb
\begin{document}
\centering
\pgfplotstabletypeset{\testtablea}\hspace{3cm}
\pgfplotstabletypeset{\testtableb}

\begin{tikzpicture}
\begin{axis} [title=automatic replacement, anchor=north east, width=7cm]
\addplot+[
  y filter/.code={\ifthenelse{\equal{#1}{}}{\def\pgfmathresult{nan}}{}},% <- added
  unbounded coords=jump
]  table {\testtablea};
\end{axis}
\hspace{1cm}
\begin{axis} [title=well defined table, anchor=north west, width=7cm]
\addplot+ [unbounded coords=jump] table {\testtableb};
\end{axis}
\end{tikzpicture}
\end{document}
1
  • There's a typo in the package name on the first line.
    – pchaigno
    Aug 22, 2017 at 8:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .