我有一个包含24行和3列的数据文件。如何仅绘制特定行的数据,例如3、7、9、14、18、21行?现在我使用以下命令:
plot 'xy.dat' using 0:2:3:xticlabels(1) with boxerrorbars ls 2
这个图表显示了全部的24条线。
我尝试了使用every
命令,但是没有找到合适的方法。
我有一个包含24行和3列的数据文件。如何仅绘制特定行的数据,例如3、7、9、14、18、21行?现在我使用以下命令:
plot 'xy.dat' using 0:2:3:xticlabels(1) with boxerrorbars ls 2
这个图表显示了全部的24条线。
我尝试了使用every
命令,但是没有找到合适的方法。
plot "<(sed -n -e 3p -e 7p -e 9p xy.dat)" using ...
另一个选择可能是注释您的数据文件,如果它似乎包含多个数据集。假设您像这样创建了数据文件:
1 2 3
2 1 3 # SetA
2 7 3 # SetB
2 2 1 # SetA SetB SetC
如果你只想使用SetA
,那么在绘图语句中使用以下sed
命令即可。
sed -ne '/SetA/s/#.*//p' xy.dat
2 1 3
2 2 1
这段话的意思是..."通常情况下,不要打印任何东西(-n
),但如果你看到包含 SetA
的行,则删除井号及其后面的所有内容并打印该行"。
或者如果你想要 SetB
,则可以使用
sed -ne '/SetB/s/#.*//p' xy.dat
2 7 3
2 2 1
或者,如果您想要整个数据文件,但去除我们的注释
sed -e 's/#.*//' xy.dat
SetB
和SetC
,请使用:sed -ne '/Set[BC]/s/#.*//p' xy.dat
2 7 3
2 2 1
plot dataf using (strcol(1)[1:1] eq "a" ? $0 : NaN):2:xticslabel(1)
如果您想跳过这些行,可以使用using语句返回“NaN”。
这是一个丑陋的hack,适用于所需行号是任意的情况:
linnum = " 1 3 7 12 16 21 "
plot dataf using (strstrt(linnum," ".int($0)." ") != 0 ? $0 : NaN):2
strstrt(a,b)
函数返回字符串b在字符串a中的位置,如果不存在则返回零。我添加了两个空格以使行号唯一。
但是我建议在这种情况下使用外部程序来预处理数据,请参见其他答案。
是的,使用every
可以解决问题。由于您想要绘制with boxerrorbars
,因此可以在plot for
循环中完成。
脚本:
### plot only certain lines appearing in a list
reset session
# create some random test data
set print $Data
do for [i=1:24] {
print sprintf("line%02d %g %g", i, rand(0)*5+1, rand(0)*0.5)
}
set print
myLines = "3 7 9 14 18 21"
myLine(i) = int(word(myLines,i)-1)
set offsets 0.5,0.5,0,0
set style fill solid 0.3
set boxwidth 0.6
set xtics out
set key noautotitle
set yrange [0:]
plot for [i=1:words(myLines)] $Data u (i):2:3:xtic(1) \
every ::myLine(i)::myLine(i) w boxerrorbars lc "blue"
### end of script
结果: