'Reading a data file with multiple blocks as one coherent data file

Suppose I have a data file that consists of multiple blocks like the following:

0 0
1 1
2 2

3 3
4 4
5 5

Now let us plot this file:

plot "data.dat" w lines

The output is two lines, with a gap from x = 2 to x = 3. However, if you remove the \n in the data file, effectively forming one block, the same command will produce a continuous plot. In a sense, when there are data blocks, gnuplot reads and interprets them separately, leading to independent plots.

Question: Is there a way to keep a file's many data blocks and yet have gnuplot read all the blocks in one coherent way, just as if the file was one single data block?

Clarification: What I refer to as "data blocks" are the STATS_blank in gnuplot.



Solution 1:[1]

A little cheat with awk:

plot "<awk -F'[ ]' '/\\S/ {printf(\"%f %f\\n\",$1,$2)}' <data.dat "  w l

will ignore blank lines. (\S <=> 'non-whitespace')

NOTE

-F'[ ]' and printf are overinsurances:

plot "<awk  '/\\S/ {print $0}' <data.dat "  w l

is almost as good as.

Solution 2:[2]

In addition to @TomSolid's awk solution, here is a platform-independent gnuplot-only version. Simply plot your data with table into another datablock, which will remove empty lines. This will work from gnuplot 5.0.0 on. You can also replace the datablocks with filenames.

Script:

### remove line break in plot caused by empty lines in datafile
reset session

$Data <<EOD
0 0
1 1
2 2

3 3
4 4
5 5
EOD

set table $DataNoGap
    plot $Data u 1:2 w table
unset table

set key top left

set multiplot layout 2,1

    plot $Data u 1:2 w lp pt 7 lc "red"
    
    plot $DataNoGap u 1:2 w lp pt 7 lc "web-green"

unset multiplot
### end of script

Result:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tom Solid
Solution 2 theozh