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?



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,



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


      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, 
      xticklabel style={rotate=90,anchor=near xticklabel},
      xticklabel=\month-\day. \hour:\minute, 
      date ZERO=2012-10-23, 
      minor tick num=12, 
      xlabel=Time / \si{\hour}, 
      ylabel=P / \si{\mm},
      \addplot+[const plot mark left,fill=none] table[x=Time,y=HourlyPrecipMM]
      \addlegendentry{Rainfall depth}
      \addplot+[const plot mark left, fill=none,dashed] table[x=Time,y=SumP]
      \addlegendentry{Mass Rainfall}
      \addplot+[const plot mark left,fill=none] table[x=Time,y=HourlyPrecipMM]
      \addlegendentry{Rainfall depth}



%%% Local Variables: 
%%% mode: latex
%%% TeX-engine: xetex
%%% TeX-master: t
%%% End: 

1 Answer 1


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).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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