Reputation: 23
I have a dat file that is split into multiple blocks, for example as the following one:
# Time (s) x (m)
0.0 0.0
1.0 1.0
2.0 2.0
3.0 3.0
4.0 4.0
5.0 5.0
5.0 10.0
6.0 11.0
7.0 12.0
8.0 13.0
9.0 14.0
10.0 15.0
The following minimal script:
filename = 'test.dat';
set terminal pngcairo size 960, 540 font 'Verdana, 20'
set output "test.png"
unset key
set xlabel "Time (s)"
set ylabel "x (m)"
set grid
set autoscale fix
plot filename u 1:2 w l lw 1.0 lc rgb 'black' notitle
Gnuplot plots two lines, one for each block. However, since the x data are discontinuous between the two blocks, a jump shows up in the plot.
I would like to join these two lines in the output plot, without modifying the data file. Is there a way to do this?
Maybe it can be done by reading the last line of each block and the first one of the next block and plotting a line between such two points, but I am not familiar with any built-in function that can do the job.
Upvotes: 2
Views: 1111
Reputation: 25938
The first approach which comes to my mind is plotting the data into a table. This will remove empty lines. Maybe there are better ways.
Script: (works with gnuplot>=5.0.0)
### remove empty lines in data
reset session
$Data <<EOD
# Time (s) x (m)
0.0 0.0
1.0 1.0
2.0 2.0
3.0 3.0
4.0 4.0
5.0 5.0
5.0 10.0
6.0 11.0
7.0 12.0
8.0 13.0
9.0 14.0
10.0 15.0
EOD
set table $Data2
plot $Data u 1:2 w table
unset table
plot $Data2 u 1:2 w lp pt 7
### end of code
Addition:
Actually, there is another approach in a single line of code (although not too obvious as well) which doesn't duplicate the data in memory (maybe only relevant for large data). You could use undefine $Data
which I guess will free the memory of $Data
.
The following single line code will give the same result as the table procedure above. The data is drawn as vectors from one datapoint to the next and hence ignoring empty lines as well.
plot x1=y1=NaN $Data u (x0=x1):(y0=y1):(x1=$1,x1-x0):(y1=$2,y1-y0) w vec nohead notitle
Addition 2:
As user @hanitors commented, if you have a .csv file and set datafile separator comma
, the datablock $Data2
will not be plotted.
You have several options:
1.0, 2.0
you can keep the default separator whitespace
because gnuplot will ignore the comma after the first number during number interpretation.1.0,2.0
you can set datafile separator comma
before plotting to the table and set datafile separator whitespace
after. $Data2
will have space as separator and will be plotted correctly.plot $Data u (sprintf("%g,%g",$1,$2)) w table
. Then $Data2
has comma as separator as well. However, this requires gnuplot>=5.2.0.Result:
Upvotes: 1