Plotting a line between two points in gnuplot

2019-07-18 07:14发布

问题:

I have a csv file with a following format having four columns (as a MWE):

xcoord1,ycoord1,xcoord2,ycoord2
0.1,0.2,0.4,0.3
0.5,0.3,0.7,0.5

I want to plot a line from each xcoord1,ycoord1 to xcoord2,ycoord2 using gnuplot. For example in this case, I would draw two lines from 0.1,0.2 to 0.4,0.3 and 0.5,0.3 to 0.7,0.5.

How is it possible?

回答1:

one solution would be to preprocess your file and generate a Gnuplot script which would employ the set arrow command. Alternatively one can plot the input file directly with a little preprocessing. The trick is to convert each line in the data file into a separate block, e.g.,

xcoord1,ycoord1,xcoord2,ycoord2
0.1,0.2,0.4,0.3
0.5,0.3,0.7,0.5

would be converted into

0.1 0.2
0.4 0.3

0.5 0.3
0.7 0.5

Gnuplot will then connect only points within one block. To achieve this, you could do:

plotCmd(fname)=sprintf("<gawk -F, 'NR>1{printf \"%%s\\t%%s\\n%%s\\t%%s\\n\\n\",$1,$2,$3,$4}' %s", fname)
plot plotCmd('input.csv') w lp


回答2:

Plot the lines as vectors without arrow heads:

plot "file" using 1:2:($3-$1):($4-$2) with vectors nohead