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.
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)
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.
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 separatorwhitespace
because gnuplot will ignore the comma after the first number during number interpretation.1.0,2.0
you canset datafile separator comma
before plotting to the table andset 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: