Nick Brodelbaum
Nick Brodelbaum

Reputation: 41

gnuplot - calculate distance between lines

Can gnuplot calculate the distance between two lines or maybe two points?

I'm having a plot where two (main) lines are plotted. For the moment let's assume that the first line is always above the second one. Is there a way to calculate the distance from line 2 to line 1 at a given x-value?

here is a picture of what my plot looks like and which distance I want to calculate:

the solid horizontal line is line 1, the dotted horizontal line is line 2

The vertical lines are just for style and have nothing to do with the actual plot, their data is stored in test.dat and test2.dat.

My data-files of the lines look like this:
line1

0       118.1
2.754   117.77
4.054   117.64
6.131   116.17
7.7     116.04
8.391   115.36
10.535  115.25
11.433  116.03
12.591  116.22
19.519  118.59

line2

19.4    118.51
15.2    116.56
10.9    115.94
10.35   114.93
9.05    114.92
8.3     115.9
5.9     116.19
4.2     116.62
2.2     117.66
-0.3    118.06

My plotting-code looks like this:

set term wxt enhanced
cd 'working directory'
unset key

set size 0.9,0.9
set origin 0.1,0.1
set title 'TITLE'

unset border
set label 21 "  rotate by 45" at -3.0,0.0 rotate by 45 point ps 2

set xrange [0:19.519]
set yrange [110:119] 
set xtics nomirror(0, 2.745, 4.054, 6.131, 7.7, 8.391, 10.535, 11.433, 12.591, 19.519) rotate by 90 offset 0,-0.1 right

set ytics "      ", 30000

plot "line1.dat" using ($1):($2):2 with labels offset 1, 1.8 rotate by 90, "line1.dat" using 1:2 with lines lt 1 lw 1 lc rgb '#000000', +112 lt 1 lw 1 lc rgb '#000000' , 'test.dat' with lines lt 1 lw 1 lc rgb '#000000', +110 lt 1 lw 1 lc rgb '#000000', 'line2.dat' with lines lt 0.5 lw 1 lc rgb '#000000', 'test2.dat' with lines lt 0.5 lw 1 lc rgb '#000000' 

Upvotes: 4

Views: 1848

Answers (2)

theozh
theozh

Reputation: 26068

An answer to this "rather old" question still might be of interest to OP, if not, maybe to others. Yes, you can calculate and plot the difference of two lines. It requires some linear interpolation. Simply assign the desired x-value to the variable myX.

Data:

SO17717287_1.dat

0       118.1
2.754   117.77
4.054   117.64
6.131   116.17
7.7     116.04
8.391   115.36
10.535  115.25
11.433  116.03
12.591  116.22
19.519  118.59

SO17717287_2.dat

19.4    118.51
15.2    116.56
10.9    115.94
10.35   114.93
9.05    114.92
8.3     115.9
5.9     116.19
4.2     116.62
2.2     117.66
-0.3    118.06

Script: (works for gnuplot>=4.6.0)

### calculating and plotting a difference between two curves
reset

FILE1 = "SO17717287_1.dat"
FILE2 = "SO17717287_2.dat"

set border 1
unset key
set origin 0.05,0.05
set size   0.9,0.8
set xrange [0:19.519]
set xtics nomirror rotate by 90 offset 0,-0.1 right
set yrange [110:119] 
unset ytics

myX = 15.2
getYa(xi) = (x0=x1, x1=$1, y0=y1, y1=$2, x1==xi ? ya=y1 : (sgn(x0-xi)!=sgn(x1-xi)) ? ya=(y1-y0)/(x1-x0)*(xi-x0)+y0 : NaN)
getYb(xi) = (x0=x1, x1=$1, y0=y1, y1=$2, x1==xi ? yb=y1 : (sgn(x0-xi)!=sgn(x1-xi)) ? yb=(y1-y0)/(x1-x0)*(xi-x0)+y0 : NaN)

set samples 2  # set to minimal possible value for plotting '+'

plot x1=y1=NaN FILE1 u 1:2:2:xtic(1) w labels offset 0,0.5 left rotate by 90, \
         '' u 1:(getYa(myX),$2) w l lc rgb 'black', \
         '' u 1:2:(0):(110-$2) w vec lt 0 nohead, \
         +112 w l lc rgb 'black', \
     x1=y1=NaN FILE2 u 1:(getYb(myX),$2) w l lt 0 lc rgb 'black', \
         '+' u (myX):(ya):(0):(yb-ya) w vec heads lc rgb "red", \
         '+' u (myX):(ya):(sprintf("%.3f",yb-ya)):xtic(sprintf("%g",myX)) w labels tc rgb "red" offset 0,1, \
         '+' u (myX):(ya):(0):(110-ya) w vec nohead lt 0 lc rgb "red"
### end of script

Result: (created with gnuplot 4.6.0)

enter image description here

Upvotes: 0

Dan Stahlke
Dan Stahlke

Reputation: 1469

You can measure the distance manually. Move the mouse to the first point and type 'r'. Then as you move the mouse around, the x and y offsets, distance and angle are displayed. Type '5' to draw a line segment and to toggle between degrees and tangent display. Zooming in beforehand increases accuracy.

By the way, typing 'h' in the plot window will display a list of keybindings to the console.

Upvotes: 2

Related Questions