在gnuplot中,在“缺少set datafile"的情况下,如何忽略两个"nan".和"-nan"? [英] In gnuplot, with "set datafile missing", how to ignore both "nan" and "-nan"?
问题描述
gnuplot命令set datafile missing "nan"
告诉gnuplot忽略数据文件中的nan
数据值.
The gnuplot command set datafile missing "nan"
tells gnuplot to ignore nan
data values in the data file.
如何同时忽略nan
和-nan
?我在gnuplot中尝试了以下内容,但是第一条语句的效果被下一条覆盖.
How to ignore both nan
and -nan
? I tried the following in gnuplot, but then the effect of the first statement is overwritten by the next.
gnuplot> set datafile missing "-nan"
gnuplot> set datafile missing "nan"
是否可以在gnuplot命令中甚至以某种方式将grep -v nan
嵌入到某种regexp中,以排除任何可想象的非数值数据?
Is it possible to somewhow embed a grep -v nan
in the gnuplot command, or even some kind of regexp to exclude any imaginable non-numerical data?
推荐答案
对于set datafile missing
,不能使用正则表达式,但是您可以使用任何程序在绘图之前过滤数据,并用一个字符替换正则表达式,例如?
设置为标记缺少的数据点.
It is not possible to use a regexp for set datafile missing
, but you can use any program to filter you data before plotting, and replacing a regexp with one character, e.g. ?
which you set to mark a missing data point.
这是一个完成您最初要求的示例:过滤-nan
,inf
等.为了进行测试,我使用了以下数据文件:
Here is an example which accomplishes, what you originally requested: filtering -nan
, inf
etc. For testing, I used the following data file:
1 0
2 nan
3 -inf
4 2
5 -NaN
6 1
并且绘图脚本可能如下所示:
And the plotting script may look like the following:
filter = 'sed -e "s/-\?\(nan\|inf\)/?/ig"'
set datafile missing "?"
set offset 0.5,0.5,0.5,0.5
plot '< '.filter.' data.txt' with linespoints ps 2 notitle
这将提供以下输出:
因此plot命令将跳过所有丢失的数据点.如果此变体不够大,则可以优化sed
过滤器,以用?
替换所有非数字值.
So the plot command skips all missing data points. You can refine the sed
filter to replace any non-numerical values with ?
, if this variant is not enough.
这很好用,但是只允许选择列,例如使用using 1:2
,但不对列进行计算,例如using ($1*0.1):2
.为此,您可以使用grep
过滤出包含nan
,-inf
等的任何行,就像在中所做的一样gnuplot缺少具有表达式评估的数据(感谢@Thiru的链接):
This works fine, but allows only to select columns e.g. with using 1:2
, but not doing computations on the columns, like e.g. using ($1*0.1):2
. To allow this, you can filter out any row, which contains nan
, -inf
etc with grep
, like its done in gnuplot missing data with expression evaluation (thanks @Thiru for the link):
filter = 'grep -vi -- "-\?\(nan\|inf\)"'
set offset 0.5,0.5,0.5,0.5
plot '< '.filter.' data.txt' with linespoints ps 2 notitle
这篇关于在gnuplot中,在“缺少set datafile"的情况下,如何忽略两个"nan".和"-nan"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!