我有一些数据存储在文本文件中,大约有10000行和2列。我知道可以使用 plot "filename.txt" using 1:2 with lines
命令容易地绘制它。但是我只想绘制从第1000行到第2000行或其他合理范围内的数据,是否有简单的方法可以实现呢?非常感谢!
看起来 gnuplot中的"every"命令 就是你要找的:
plot "filename.txt" every ::1000::2000 using 1:2 with lines
或者,预处理文件以选择您感兴趣的行。例如,使用awk:
awk "NR>=1000 && NR<=2000" filename.txt > processed.txt
然后在现有的gnuplot命令/脚本中使用生成的 "processed.txt"。
更简单:
plot "<(sed -n '1000,2000p' filename.txt)" using 1:2 with lines
使用伪列0,您可能可以削减对外部实用程序的依赖(例如,如果您的系统没有安装它们)。
请参阅help plot datafile using pseudocolumn
尝试类似以下内容:
LINEMIN=1000
LINEMAX=2000
#create a function that accepts linenumber as first arg
#an returns second arg if linenumber in the given range.
InRange(x,y)=((x>=LINEMIN) ? ((x<=LINEMAX) ? y:1/0) : 1/0)
plot "filename.txt" using (InRange($0,$1)):2 with lines
(在Linux上测试过Gnuplot 4.4.2)
cutoff(c1,c2,xmin,xmax) = (c1>=xmin)*(c1<=xmax) ? c2 : NaN
plot "data.txt" u 1:(cutoff(($1),($2),1000,2000))
我建议使用一些命令行工具,例如sed
、grep
或bash
。在你的例子中
head -n 2000 ./file.data > temp.data
并且
tail -n 1000 temp.data > temp2.data
可能会起作用。但是还没有测试过这样大的数字是否与头部和尾部兼容。