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.

I am trying to plot data from a file, using pgfplots for the plot and pgfplotstable to read in the data and calculate a new column (cummulative values from another row), which is plotted as well. My data sometimes has bad values (negative values when all values should be non-negative) and I would like to filter those values from the plot and the calculation, either by ignoring them or setting them to zero. pgfplots has a convenient filter option for the plots, but this has no effect on the calculated column.

Here's a MWE of what I am trying to accomplish. There should not be any negative values in the plots. How should I filter the values?

\documentclass{minimal}

 \usepackage{filecontents}
 \begin{filecontents}{data.csv}


    Time,TemperatureC,DewpointC,PressurehPa,WindDirection,WindDirectionDegrees,WindSpeedKMH,WindSpeedGustKMH,Humidity,HourlyPrecipMM,Conditions,Clouds,dailyrainMM,SoftwareType,DateUTC,
2012-10-26 00:51:00,26.9,26.1,1009.4,SSE,163,8.0,22.5,95,0.0,,,0.0,weatherlink.com 1.10,2012-10-26 04:51:00, 
2012-10-26 01:06:00,26.9,26.1,1009.0,SW,231,8.0,20.9,94,0.0,,,0.0,weatherlink.com 1.10,2012-10-26 05:06:00, 
2012-10-26 01:21:00,26.9,26.1,1009.0,SSE,151,14.5,-1607.4,94,-2539.7,,,0.0,weatherlink.com 1.10,2012-10-26 05:21:00, 
2012-10-26 01:36:00,27.0,26.1,1009.0,SSE,155,4.8,22.5,93,0.0,,,0.0,weatherlink.com 1.10,2012-10-26 05:36:00, 
2012-10-26 01:51:00,27.1,26.1,1009.4,South,174,6.4,19.3,93,0.0,,,0.0,weatherlink.com 1.10,2012,
 \end{filecontents}

% UNITS
 \usepackage{siunitx}

% PGFPLOTS and TABLES
 \usepackage{tikz}
 \usepackage{pgfplots}
   \pgfplotsset{
     width=10cm,
     compat=1.7}
   \usepgfplotslibrary{dateplot}
   \usepgfplotslibrary{groupplots}

\usepackage{pgfplotstable}
   \pgfplotstableset{
     col sep=comma,
     create on use/SumP/.style={
       create col/expr={\pgfmathaccuma + \thisrow{HourlyPrecipMM}}
       }
   }
% ------------------------------------------------- Document starts here
\begin{document}

\pgfplotstableread{data.csv}\data



  \begin{tikzpicture}
    \begin{groupplot}[
      group style={%
        group size=1 by 2,
        horizontal sep=0pt,
        vertical sep=10pt,
        xticklabels at=edge bottom,
        xlabels at=edge bottom,
        ylabels at=edge left,
      },
      legend style={draw=none}, 
      legend pos=north west, 
      legend cell align=left, 
      date coordinates in=x, 
      xtick=,
      xticklabel style={rotate=90,anchor=near xticklabel},
      xticklabel=\month-\day. \hour:\minute, 
      date ZERO=2012-10-23, 
      minor tick num=12, 
      footnotesize,
      width=10cm,
      xlabel=Time / \si{\hour}, 
      ylabel=P / \si{\mm},
      ybar
      ]
      \nextgroupplot[]
      \addplot+[const plot mark left,fill=none] table[x=Time,y=HourlyPrecipMM]
      {\data};
      \addlegendentry{Rainfall depth}
      \addplot+[const plot mark left, fill=none,dashed] table[x=Time,y=SumP]
      {\data}; 
      \addlegendentry{Mass Rainfall}
      \nextgroupplot[ymax=100,height=4cm]
      \addplot+[const plot mark left,fill=none] table[x=Time,y=HourlyPrecipMM]
      {\data};
      \addlegendentry{Rainfall depth}

    \end{groupplot}
  \end{tikzpicture}

\end{document}

%%% Local Variables: 
%%% mode: latex
%%% TeX-engine: xetex
%%% TeX-master: t
%%% End: 
share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

For accumulating only the non-negative values, you can use the expression \pgfmathaccuma + max(\thisrow{HourlyPrecipMM},0), where max(\thisrow{HourlyPrecipMM},0) evaluates to the data value if it's non-negative, and 0 if it's negative.

For filtering negative coordinates from the plots, you can set the key restrict y to domain=0: (leaving the upper bound empty).

share|improve this answer
    
This worked perfectly. Thanks! –  Ricardo Nov 9 '12 at 12:24
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.