Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Hi there I got the following problem at hand. I load my plot data automatically from an external .csv file and manipulate it via a simple expression (conversion from ms to seconds) before plotting it via pgfplots. My problem arises whenever I have empty values to plot, the expression returns

Package PGF Math Error: Could not parse input '' as a floating point number, sorry. The unreadable part was near ''. (in '/1000').

For better understanding the MWE

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents}{testdata.csv}
a;b;c
5000;;2000
\end{filecontents}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[   
    legend entries = {
      \textbf{a},
      \textbf{b},
      \textbf{c},
    }
  ]  
  \addplot table[x expr=\coordindex,y expr=\thisrow{a}/1000, col sep=semicolon]{testdata.csv};
  \addplot table[x expr=\coordindex,y expr=\thisrow{b}/1000, col sep=semicolon]{testdata.csv};
  \addplot table[x expr=\coordindex,y expr=\thisrow{c}/1000, col sep=semicolon]{testdata.csv};

  \end{axis}
\end{tikzpicture}

\end{document}

In this example column b has the empty value which results in the error because of the expression. My question is how can I get around that error without inserting NaN in the empty space.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

You might consider using a y filter instead of a y expr, as the latter does not seem to be able to cope with missing values. Before doing the calculation, one has to test if the current point is missing/empty or not. I illustrate this below, which at least works if you want to transform all the plots in the axis in the same way.

Note that some of Harish Kumar's suggestions of using \pgfplotsset{compat=1.7} and reading the table into a macro may be a good idea instead of reading the file three times etc. Here, I have made only minimal changes to your provided MWE.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents}{testdata.csv}
a;b;c
5000;;2000
\end{filecontents}

\def\myemptymacro{}
\newcommand*{\divideathousand}[1]
        {\ifx\pgfmathresult\myemptymacro\else\pgfmathdivide{#1}{1000}\fi}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[   
    legend entries = {
      \textbf{a},
      \textbf{b},
      \textbf{c},
    },
    y filter/.code={\divideathousand{#1}}
  ]
  \addplot table[x expr=\coordindex,y=a, col sep=semicolon]{testdata.csv};
  \addplot table[x expr=\coordindex,y=b, col sep=semicolon]{testdata.csv};
  \addplot table[x expr=\coordindex,y=c, col sep=semicolon]{testdata.csv};


  \end{axis}
\end{tikzpicture}

\end{document}

Result:

illustration of output

Note that the legend is incorrect though, because no points were kept for the second plot...

share|improve this answer
    
Awesome that´s what I was looking for, a check for empty values before the division. Thanks a lot. –  chris Nov 24 '12 at 15:22
add comment

I do not understand why you want to plot empty values. However, if you choose to use 0 instead of empty values, here is an automatic suggestion using pgfplotstable. We can replace empty cells with 0 and 0/1000 should not produce NaN errors as the result is defined (i.e., 0)

Code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\usepackage{pgfplotstable}
\pgfplotstableset{col sep=semicolon}
\usepackage{filecontents}

\begin{filecontents*}{testdata.csv}
a;b;c
5000;;2000
\end{filecontents*}
\pgfplotstableread{testdata.csv}\loadedtable

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    legend entries = {
      \textbf{a},
      \textbf{b},
      \textbf{c},
    }
  ]
  \addplot table[x expr=\coordindex,y expr=\thisrow{a}/1000, col sep=semicolon]{\loadedtable};
  \addplot table[x expr=\coordindex,y expr=\thisrow{b}/1000, col sep=semicolon]{\loadedtable};
  \addplot table[x expr=\coordindex,y expr=\thisrow{c}/1000, col sep=semicolon]{\loadedtable};

  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
1  
I don´t want to plot empty values I want them skipped, when inserting 0 they are plotted as 0, which I don´t want. For better understanding, I want the same behaviour as doing a simple y = a, which skips the values when they are empty. But with the expression whenever I got an empty value it gives me the input parse error. –  chris Nov 22 '12 at 13:55
    
@Harish Kumar: If you load the table first using \pgfplotstableread, the missing coordinate is plotted as 0 even without the empty cells with={0}. In fact, empty cells with has no effect on the table when it's used within a plot, it only affects table output using \pgfplotstabletypeset. –  Jake Nov 23 '12 at 0:01
    
@Jake: Changed in the answer. Thanks. –  Harish Kumar Nov 23 '12 at 0:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.