Plotting arrows with start and end from two different files

百般思念 提交于 2020-01-24 21:48:53

问题


I have two different .txt files with x and y coordinates of equal number of samples in both.

File 1
x y
1 2
5 4
4 6

File 2 
x y
5 6
3 4 
2 3

I want to connect each of these points inFile 1 with the corresponding points in File 2. I know to draw an arrow between two points it is

set arrow from (x,y) to (c,d)

But how do I get the coordinates of these points from two different files to draw connecting lines/ arrows?


回答1:


Something like this:

plot "< paste file1.data file2.data" with vectors



回答2:


Although, the fastest and shortest way to merge columns from different files is probably via an external tool, I nevertheless summarize 3 gnuplot-only (i.e. platform independent) methods for merging columns from different files/datasets.

  • Method 1 is apparently very slow.
  • Method 2 is somewhat better but a bit confusing, approx. 5-6 times faster than Method 1
  • Method 3 works only with gnuplot >=5.2 because it uses arrays, approx. 20-30 times faster than Method 1

Improvements and comments are welcome.

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


来源:https://stackoverflow.com/questions/53726708/plotting-arrows-with-start-and-end-from-two-different-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!