我可以只使用gnuplot标题中数据文件中的某些数字吗?

时间:2012-06-06 18:08:46

标签: gnuplot

我有一个包含许多数据列的数据文件,前两行如下所示:

#time a1 b1 c1 d1 a2 b2 c2 d2 
1 2 3 4 5 6 7 8 9

我想在图表上标题如下:

a1=2, a2=6, b1=3, b2=7, c1=4, c2=8, d1=5, d2=9

所以,基本上只是从第一行获取数据并添加一些文本。这可能吗?

谢谢mgilson!这是脚本(第一部分。)

#filename = "a6_a.txt"

//defining variables called ofac and residual

unset multiplot
set term aqua enhanced font "Times-Roman,18" 

set lmargin 1
set bmargin 1
set tmargin 1
set rmargin 1
set multiplot
set size 0.8,0.37

set origin 0.1,0.08  #bottom
 set xlabel "time" offset 0,1  #bottom

 set ylabel "{/Symbol w}_i - {/Symbol w}_{i+1}"
 set yrange [-pi:pi]
 plot filename using 1:(residual($7 -$14)) ti "" pt 1 lc 2, \
            "" using 1:(residual($14-$21)) ti "" pt 1 lc 3, \
            "" using 1:(residual($21-$28)) ti "" pt 1 lc 4

set origin 0.1,0.36 #mid
 set format x ""    #mid, turn off x labeling
 set xlabel ""      #mid
 set ylabel "P_{i+1}/P_i" offset 2,0
 set yrange [*:*]
 plot filename using 1:(($10/ $3)**1.5) ti "P_2/P_1" pt 1 lc 2, \
            "" using 1:(($17/$10)**1.5) ti "P_3/P_2" pt 1 lc 3, \
            "" using 1:(($24/$17)**1.5) ti "P_4/P_3" pt 1 lc 4


set origin 0.1,0.64 #top
 set ylabel "semi-major axes"
 set yrange [*:*]
 plot filename using 1:($3):($4*$3) with errorbars ti "" pt 1 lc 1, \
   "" using 1:($10):($10*$11) with errorbars ti "" pt 1 lc 2, \
   "" using 1:($17):($17*$18) with errorbars ti "" pt 1 lc 3, \
   "" using 1:($24):($24*$25) with errorbars ti "" pt 1 lc 4

unset multiplot
unset format 
unset lmargin
unset bmargin
unset rmargin
unset tmargin

1 个答案:

答案 0 :(得分:0)

你可能只能使用gnuplot(我认为)这样做,但这会很痛苦。最简单的解决方案是编写一个外部脚本,为您格式化标题 - 例如在python中(未经测试):

#python script formatTitle.py
import sys
fname=sys.argv[1]
with open(fname,'r') as f:
    line1=f.readline().split()
    line2=f.readline().split()

pairs=sorted(zip(line1,line2)[1:])
print (", ".join("%s=%s"%(p[0],p[1]) for p in pairs))

然后在gnuplot:

set title "`python formatTitle.py datafile.dat`"    

修改

上面的脚本有一些问题。首先,//不是gnuplot中的注释字符 - 因此会立即引发某种语法错误。接下来,您使用filename作为字符串变量(可以),但您没有在任何地方定义它。 (你的脚本的第一行可能会工作,除了它已被注释掉)。最后,我无法检查,因为我没有您的数据文件,但您使用lmargin etc + set size + set origin对我来说有点难以跟踪。聪明的人可能没有问题。但是,当我想明确地对齐我的图时,我使用margin的第二种形式。

#make plots go from 0.1 to 0.9 on the screen (screen range is 0-1)
# (0,0) is the lower left corner, (1,1) is the upper right.
set lmargin at screen 0.1
set rmargin at screen 0.9 

然后对于每个子图,你可以这样做:

NPLOTS=3
SCREENSIZE=0.8  #leave .1 for the top and bottom border
DY=SCREENSIZE/NPLOTS #height of each plot

#bottom
set tmargin at screen 0.9-2*DY
set bmargin at screen 0.9-3*DY
#other commands
plot ... 

#middle
set tmargin at screen 0.9-DY
set bmargin at screen 0.9-2*DY
#other commands ...
plot ...

#top
set tmargin at screen 0.9
set bmargin at screen 0.9-DY
#other commands ...
plot ...

当然,您可以按照自己的意愿来处理这些职位。