Search code examples
functionvectorgnuplotpoints

GNUPLOT: Joining different series of points with vectors


I have a file with data in 2 columns X and Y. There are some blocks and they are separated by a blank line. I want to join the points (given by their coordenates x and y in the file) in each block using vectors. I'm trying to use these functions:

prev_x = NaN
prev_y = NaN
dx(x) = (x_delta = x-prev_x, prev_x = ($0 > 0 ? x : 1/0), x_delta)
dy(y) = (y_delta = y-prev_y, prev_y = ($0 > 0 ? y : 1/0), y_delta)

which I've taken from Plot lines and vector in graphical gnuplot (first answer). The command to plot would be plot for[i=0:5] 'Field_lines.txt' every :::i::i u (prev_x):(prev_y):(dx($1)):(dy($2)) with vectors. The output is enter image description here and the problem is that the point (0,0) is being included even though it's not in the file. I don't think I understand what the functions dx and dy do exactly and how they are being used with the option using (prev_x):(prev_y):(dx($1)):(dy($2)) so an explanation of this would help me a lot to try to fix this. This is the file:

#1
0   5   
0   4   
0   3   
0.4 2   
0.8 1   
0.8 1   

#2
2   5
2   4
2   3
2   2
2   1
2   0

#3
4   5
4.2 4
4.5 3
4.6 2
4.7 1
4.7 0

#4
7   5
7.2 4
7.5 3
7.9 2
7.9 1
7.9 0

#5 
9   5
9   4
9.2 3
9.5 2
9.5 1
9.5 0

#6
11  7
12  6
13  5
13.3    4
13.5    3
13.5    2
13.6    1
14  0

Thanks!


Solution

  • I'm not completely sure, what the real problem is, but I think you cannot rely on the columns in the using statement to be evaluated from left to right, and your check $0 > 0 in the dx and dy some too late in my opinion.

    I usually put all the assignments and conditionals in the first column, and that works fine also in your case:

    set offsets 1,1,1,1
    unset key
    prev_x = prev_y = 1
    
    plot for [i=0:5] 'Field_lines.txt' every :::i::i \
        u (x_delta = prev_x-$1, prev_x=$1, y_delta=prev_y-$2, prev_y=$2, ($0 == 0 ? 1/0 : prev_x)):(prev_y):(x_delta):(y_delta) with vectors backhead
    

    Also, to draw a vector from j-th row to the point in the following row you must invert the definition of x_delta and use backhead to draw the vectors in the correct direction

    enter image description here