0

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?

1
  • Just as extra hint. When I am using the command plot 'file' u x1:x2 every ::3::3 w p I am getting an output. it plots line 3 of every block on a single plot? But If I change the option w p to w l nothing appears. Looks like the issue was also reported on stackoverflow Commented Nov 10, 2018 at 9:58

1 Answer 1

2

(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:

  1. plot the selected data into a table (datablock) and plot this datablock. (Simpler plot command but probably more memory usage).

  2. 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.

  3. 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:

enter image description here

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:

enter image description here

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
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply. Still some incomprehensions. Can you explain these two instructions 0:3:4 and ::0::0?
As it was not already complicated, I am also, I am looping through many files of the same kinds. I am using text file for set table "name_i.dat", where I generate name_i.dat into a do for loop using sprintf("name_%d", i). Is there a way to do the same with something that looks like $DATAi (ref. your code)? .
I didn't know how your data exactly looks like, so I assumed something. 0 is a "pseudocolumn", in gnuplot check help pseudcolumns. ::0::0 is the first dataline of each block, check help every. The "trick" is to plot the desired data into a new table (i.e. datablock). In my test data example plot $Data u 3:4 every ::0::0 with table and later plot $Data2 u 1:2 w lp would also work.
if you wan to do this with several files... see addition in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.