Gnuplot从数据文件绘制特定行的图表

3

我有一个包含24行和3列的数据文件。如何仅绘制特定行的数据,例如3、7、9、14、18、21行?现在我使用以下命令:

plot 'xy.dat' using 0:2:3:xticlabels(1) with boxerrorbars ls 2

这个图表显示了全部的24条线。

我尝试了使用every命令,但是没有找到合适的方法。

3个回答

3
未经测试,但大概是这样的。
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

如果你想要使用SetBSetC,请使用:
sed -ne '/Set[BC]/s/#.*//p' xy.dat
2 7 3 
2 2 1

2
如果您想要的行有共同点可以进行评估,例如第1列中的标签以"a"开头。
 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中的位置,如果不存在则返回零。我添加了两个空格以使行号唯一。

但是我建议在这种情况下使用外部程序来预处理数据,请参见其他答案。


1

是的,使用every可以解决问题。由于您想要绘制with boxerrorbars,因此可以在plot for循环中完成。

  • 没有外部工具,即仅限于gnuplot,因此与平台无关
  • 没有严格递增的行号,但可以使用任意序列的行

脚本:

### 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

结果:

enter image description here


非常优雅...做得好! - Mark Setchell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接