我有两个不同的.txt文件,它们的x和y坐标均相等。
File 1
x y
1 2
5 4
4 6
File 2
x y
5 6
3 4
2 3
我想将文件1中的每个点与文件2中的相应点连接起来。我知道在两个点之间绘制一个箭头
set arrow from (x,y) to (c,d)
但是如何从两个不同的文件中获取这些点的坐标以绘制连接线/箭头?
答案 0 :(得分:0)
类似这样的东西:
plot "< paste file1.data file2.data" with vectors
答案 1 :(得分:0)
尽管最快和最短的合并不同文件中的列的方法可能是通过外部工具,但我总结了3种仅gnuplot(即独立于平台)的方法来合并来自不同文件/数据集的列。
欢迎改进和评论。
### merging columns from different files/datasets with gnuplot only
reset session
# creating some dummy data
TimeStart = time(0.0)
N = 5000
set table $Data1
set samples N
plot '+' u (rand(0)):(rand(0)) with table
unset table
set table $Data2
set samples N
plot '+' u (rand(0)):(rand(0)) with table
unset table
TimeEnd = time(0.0)
print sprintf("Creating data: %.3f sec",TimeEnd-TimeStart)
# end creating dummy data
# Method 1
TimeStart = time(0.0)
stats $Data1 nooutput
RowCount = STATS_records
set print $FinalData1
set table $DataM1_1
do for [i=0:RowCount-1] {
plot $Data1 u (a=$1,$1):(b=$2,$2) every ::i::i w table
plot $Data2 u (c=$1,$1):(d=$2,$2) every ::i::i w table
print sprintf("%g\t%g\t%g\t%g",a,b,c,d)
}
unset table
set print
TimeEnd = time(0.0)
print sprintf("Method 1: %.3f sec",TimeEnd-TimeStart)
# print $FinalData1
# end Method 1
# Method 2
TimeStart = time(0.0)
set table $DataM2_1
plot for [i=1:2] $Data1 u ($0+i*0.1):i w table
plot for [i=1:2] $Data2 u ($0+i*0.3):i w table
unset table
set table $DataM2_2
plot $DataM2_1 u 1:2 smooth frequency
unset table
x1 = y1 = x2 = y2 = NaN
Shift(x) = (x1 = y1, y1 = x2, x2 = y2, y2 = x)
set table $DataM2_3
plot $DataM2_2 u (Shift($2),x1):(y1):(x2):(y2) with table
unset table
set table $DataM2_4
plot $DataM2_3 u 1:2:3:4 every 4::3 with table
unset table
set table $FinalData2 # remove empty lines
plot $DataM2_4 u 1:2:3:4 every ::0 with table
unset table
TimeEnd = time(0.0)
print sprintf("Method 2: %.3f sec",TimeEnd-TimeStart)
# print $FinalData2
# end Method 2
# Method 3
# requires gnuplot >=5.2
# get number of rows, assuming to be identical for $Data1 and $Data2
TimeStart = time(0.0)
stats $Data1 nooutput
RowCount = STATS_records
array Col1[RowCount]
array Col2[RowCount]
array Col3[RowCount]
array Col4[RowCount]
set table $DataM3_1
plot $Data1 u (Col1[$0+1]=$1,0):(Col2[$0+1]=$2,0) w table
plot $Data2 u (Col3[$0+1]=$1,0):(Col4[$0+1]=$2,0) w table
unset table
set print $FinalData3
do for [i=1:RowCount] {
print sprintf("%g\t%g\t%g\t%g",Col1[i],Col2[i],Col3[i],Col4[i])
}
set print
TimeEnd = time(0.0)
print sprintf("Method 3: %.3f sec",TimeEnd-TimeStart)
# print $FinalData3
# end Method 3
### end of code