In gnuplot, with “set datafile missing”, how to ignore both “nan” and “-nan”?

爷,独闯天下 提交于 2019-12-03 07:47:40
Christoph

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.

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

This gives the following output:

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!