![使用 pgfplotstable 读取和计算数据时按值过滤](https://linux22.com/image/257994/%E4%BD%BF%E7%94%A8%20pgfplotstable%20%E8%AF%BB%E5%8F%96%E5%92%8C%E8%AE%A1%E7%AE%97%E6%95%B0%E6%8D%AE%E6%97%B6%E6%8C%89%E5%80%BC%E8%BF%87%E6%BB%A4.png)
我正在尝试绘制文件中的数据,用于pgfplots
绘图并pgfplotstable
读取数据并计算新列(来自另一行的累积值),该列也将被绘制。我的数据有时会出现错误值(所有值都应该为非负值时却出现了负值),我想从绘图和计算中过滤掉这些值,方法是忽略它们或将它们设置为零。pgfplots
有一个方便的绘图过滤选项,但这对计算列没有影响。
这是我想要实现的 MWE。图中不应该有任何负值。我应该如何过滤这些值?
\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:
答案1
为了仅累积非负值,您可以使用表达式\pgfmathaccuma + max(\thisrow{HourlyPrecipMM},0)
,其中max(\thisrow{HourlyPrecipMM},0)
如果数据为非负则计算数据值,0
如果数据为负则计算数据值。
为了从图中过滤负坐标,您可以设置键restrict y to domain=0:
(将上限留空)。