I have a data file with the following structure
block1: line 1
line 2
line 3
.....
block2: line 1
line 2
line 3
......
block3: .....
To plot only the block2, I use the command
plot 'file' u x1:x2 every :::2::2 w l
How to gather only line 1 of each block on the plot
command?
(Revised versions)
If you have empty lines in your data, gnuplot will separate the plotted line. So, if you are selecting just the first line of each block you could plot this with points
and with linespoints
, but when plotted with lines
you won't see anything because all lines are disconnected.
Three possible solutions:
plot the selected data into a table (datablock) and plot this datablock. (Simpler plot command but probably more memory usage).
plot your data with vectors
from one datapoint to the next. (No extra memory (datablock) required, but more complex plot command). This looks like with lines
. If you want with linespoints
you need to plot the same data a second time with points
.
you could filter your data and remove empty lines with an external tool and plot the filtered data. However, if possible, I prefer gnuplot-only solutions because they are platform-independent.
Data: SO53223558.dat
# block line x y
0 0 1 0
0 1 2 1
0 2 3 2
0 3 4 3
1 0 5 10
1 1 6 11
1 2 7 12
1 3 8 13
2 0 9 20
2 1 10 21
2 2 11 22
2 3 12 23
Script 1: (requires gnuplot>=5.0.0, because of datablocks)
### plot values of different blocks connected with lines
reset session
FILE = "SO53223558.dat"
set table $Data
plot FILE u 3:4 every ::0::0 w table
unset table
set key top left
plot FILE u 3:4 w lp pt 7 lc "grey" ti "all data",\
$Data u 1:2 w lp pt 7 lc "green" lw 2 ti "1st of each block"
### end of script
Result 1:
Script 2: (requires gnuplot>=4.4.0, March 2010)
### plot values of different blocks connected with lines
reset
FILE = "SO53223558.dat"
set key top left
plot FILE u 3:4 w lp pt 7 lc rgb "grey" ti "all data",\
x1=y1=NaN FILE u (x0=x1,x1=$3):(y0=y1,y1=$4):(x0-x1):(y0-y1) every ::0::0 \
w vec lc rgb "blue" lw 2 nohead ti "1st of each block"
### end of script
Result 2:
Addition:
If you want to do this with several files, try the following example below.
The datafiles should be named SO53223558_n.dat
where n
is from 1 to 3. No data and output is shown here.
Script: (works with gnuplot>=5.0.0)
### plot every Nth line of all blocks of several systematic files
reset session
count = 3 # number of files
N = 0 # Nth line of each block, index is zero based
set table $Data
do for [i=1:count] {
FILE = sprintf("SO53223558_%d.dat", i)
plot FILE u 3:4 every ::N::N w table
}
unset table
plot $Data u 1:2 w lp pt 7 lc "red"
### end of script