1

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?

Christoph
  • 47,569
  • 8
  • 87
  • 187

2 Answers2

1

Something like this:

plot "< paste file1.data file2.data" with vectors
Gavin Portwood
  • 1,217
  • 8
  • 9
  • 1
    +1, The `paste` command is certainly efficient if you are under Linux or if you have CoreUtils from GnuWin under Windows. But to be precise, this would not draw the OP's desired arrows, but `plot "< paste file1.data file2.data" u 1:2:($3-$1):($4-$2) with vectors` would. – theozh Sep 20 '22 at 12:41
0

Edit: I came across this old answer and was amazed (actually shocked) how inefficient and complicated solutions I suggested.

A much better approach: Since gnuplot 5.2.0 you can index datablocks. Prerequisite is that your data is in a datablock already. See here: gnuplot: load datafile 1:1 into datablock. If you can be sure that your data files have identical number of lines, you can "mimic" the Linux paste command using a gnuplot-only, hence, platform-independent solution.

Basically, you join the two lines having the corresponding index after removing the last character (newline) of each line.

Script: (works with gnuplot>=5.2.0, Sept. 2017)

### plot arrows across two files, i.e. merge lines with gnuplot only
reset session

$Data1 <<EOD
File 1
x y
1 2
5 4
4 6
EOD

$Data2 <<EOD
File 2 
x y
5 6
3 4
2 3
EOD

set print $Combined
    do for [i=1:|$Data1|] {
        print $Data1[i][1:strlen($Data1[i])-1].' '.$Data2[i][1:strlen($Data2[i])-1]
    }
set print

plot $Combined u 1:2:($3-$1):($4-$2) w vec lc rgb "red"
### end of script

Result:

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72